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() } }; });
if value of attribute example-number
hard-coded, suggest using $eval
once, , storing value. variable num
have correct type (a number).
Comments
Post a Comment