angularjs - Behavior of controller inside directives -


i know $scope controller can shared link function in directives.

for example, in code can call function declared controller print 'hello world' on browser console:

 .directive('mydirective', [function () {         return {             restrict : 'e',             replace : true,             controller: 'mycontroller',             templateurl : 'directives/mydirective.tpl.html',             link : function (scope, elem, attrs, controller) {                 scope.message = 'hello world!';             }         };     }])      .controller('mycontroller', [function ($scope, $element, $attrs, $log, $timeout) {          // $timeout wait link function ready.         $timeout(function () {             // prints hello world expected.             $log.debug($scope.message);          });           });     }]) 

ok, works fine.

my questions are:

  1. in approach, same scope shared between controller , directive?
  2. what consequences use approach? let assume not manipulate dom elements in controller, in link function.
  3. i need avoid manipulate dom elements in controller? if $scope, $elem, etc same?

these questions didn't find on angular directive documentation.

here's plunker sample code.

in approach, same scope shared between controller , directive?

yes, is.

what consequences use approach? let assume not manipulate dom elements in controller, in link function.

the controller provides directive's behavior, regular angular application. said, should manipulate scope inside controller function only. if need change scope link function, call method of it. besides, since controller executed before link function, should initialized scope in former latter can valid model work on.

i need avoid manipulate dom elements in controller? if $scope, $elem, etc same?

by definition, link function place perform dom manipulation. can't find technical reason prevent manipulating dom inside directive's controller except shouldn't. in fact, in order check i've changed 1 directive i've written , moved code link function controller function , everything's kept working. if mix both scope logic , dom manipulation think it'll hard track down what's going on.

finally, may find article useful: understanding directives.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -