javascript - How do I pass multiple attributes into an Angular.js attribute directive? -


i have attribute directive restricted follows:

 restrict: "a" 

i need pass in 2 attributes; number , function/callback, accessing them within directive using attrs object.

if directive element directive, restricted "e" this:

<example-directive example-number="99" example-function="examplecallback()"> 

however, reasons won't go need directive attribute directive.

how pass multiple attributes attribute directive?

the directive can access attribute defined on same element, if directive not element.

template:

<div example-directive example-number="99" example-function="examplecallback()"></div> 

directive:

app.directive('exampledirective ', function () {     return {         restrict: 'a',   // 'a' default, remove line         scope: {             callback : '&examplefunction',         },         link: function (scope, element, attrs) {             var num = scope.$eval(attrs.examplenumber);             console.log('number=',num);             scope.callback();  // calls examplecallback()         }     }; }); 

fiddle

if value of attribute example-number hard-coded, suggest using $eval once, , storing value. variable num have correct type (a number).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -