Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular communication between controllers and directives

I have this piece of code which allows a user to leave comments on a list of items. I created a directive and listen to keydown in order to let the user submit a comment if keyCode == 13.

Not sure if I should include the code to post a comment within the directive. What is the best way to communicate between controllers and directives?

I also check whether or not the input is empty before submitting the comment. It works but not sure this is Angular best practice?

Here is my plunker.

like image 206
John Avatar asked May 13 '26 18:05

John


1 Answers

you don't need to write a directive, if you want to use ng-keydown..

example:

template:

<input type="text" ng-model="myText" ng-keydown="checkKeyCode($event)">

controller: -- written in coffeescript

$scope.checkKeyCode = ($event)->
  if $event.keyCode == 13 and $scope.myText?
    $scope.doSomething()
like image 64
giggles and smiling wrinkles Avatar answered May 19 '26 04:05

giggles and smiling wrinkles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!