Umer Umer - 5 months ago 11
AngularJS Question

Accessing Directive attribute value inside the Template

I have created a directive that is included in the html as follow:

<w3-test-directive id="first" url="tableParams" sql="select *from table1"></w3-test-directive>

<w3-test-directive id="second" url="tableParams2" sql="select *from table2"></w3-test-directive>


Now in my directive Template i want the value of attribute id.
How can I get that?

My template is as follow:

template: [
'<p>The attribute value is {{value of id attribute}}<p>'
]


so the template would look some thing like this:

"<p>The attribute value is first<p>"

Answer

Can use template function which provides arguments element and attributes.

template : function(element, attrs){  
   return  '<p>ID is ' + element[0].id +'<p>'
}