i have seen people binding click in there directive's link function on ng-click.
i have researched long time , haven't found answer. can give me reason?
i have been using ng-click because easier creating new directive,and can overview in glance.
example
how write directive
html
<some-directive> <button ng-click="somefunction()">lorem ipsum</button> </some-directive>
javascript
angular.module('myapp',[]) .directive('somedirective',function(){ return { restrict:'e', scope:{}, link: function (scope,element,attrs) { //code goes here scope.somefunction = function() { //callback } //more code goes here } } })
how people write directive
html
<some-directive> <button click-trigger>lorem ipsum</button> </some-directive>
javascript
angular.module('myapp',[]) .directive('somedirective',function(){ return { restrict:'e', scope:{}, link: function (scope,element,attrs) { //code goes here } } }) .directive('click-trigger',function($rootscope) { return { restrict:'e', link: function(scope,element,attrs) { element.bind('click',callbackfunction); function callbackfunction () { //callback } } } })
more angular way in view include nested elements in template.
.directive('foo', function(){ return { template:'<button ng-click="clickme()">click me</button>' } })
the problem of approach nested dom elements belong scope of directive - not scope of controller. can run in confusion if trying create isolated scopes.
please note angular behaves in odd ways when comes isolated scopes. unnecessarily complicated in view.
please study this question.
Comments
Post a Comment