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

http://jsfiddle.net/pca2g/1/

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

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -