How do I avoid AngularJS in inline javascript calls? -
i new angularjs , mv* patterns in javascript. i've been playing around angularjs on project , curious how avoid situation doing things <a ng-click="dosomething()"></a>
? don't javascript in html. want able control javascript javascript files rather html files. there better way handle type of stuff along lines of how can specify class or id supposed have behavior associated in angularjs?
you can encapsulate own behavior using directives , setup bindings events on elements call function if want wrap behavior (i believe read ben nadel angularjs blogger does... http://www.bennadel.com/blog/2446-using-controllers-in-directives-in-angularjs.htm).
as stated in comment though don't find issue, if on larger team potentially see desire leave binding javascript developer , not have html developer worry details , use directives seeing how write both parts don't have issue.
directives way teach html new tricks. during dom compilation directives matched against html , executed. allows directives register behavior, or transform dom.
http://docs.angularjs.org/guide/directive
html
<body ng-app="myapp"> <div clickable>click me</div> <button clickable>click me</button> </body>
javascript
angular.module("myapp",[]).directive("clickable", function() { return { restrict: 'a', link: function(scope, ielem, iattrs) { ielem.bind("click", function() { alert("holy guacamole"); }); } } });
Comments
Post a Comment