angularjs - Access local scope variable inside template as a variable instead of string -


i'm creating directive have scope(local) variable type , value should return appropriate template. choosetemplatebytype returns string appropriate template based on type text_field example.

template:   choosetemplatebytype(type) 

the question how access variable type of local scope can pass value function choosetemplatebytype

the best way achieve pass type variable directive via attribute.

html

<my-directive type="foo"></my-directive> 

js

myapp.directive('mydirective', function() {   return {     template: function(elem, attr){       return "<h1>" + attr.type + "</h1>"; // output <h1>foo</h1>     }   }; }); 

or

myapp.directive('mydirective', function() {   return {     templateurl: function(elem, attr){       return attr.type + '.html'; // load foo.html     }   }; }); 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -