Monday, 12 August 2013

AngularJS: Create new scope variable name using a variable value

AngularJS: Create new scope variable name using a variable value

I would like to create a new scope variable using the value of a variable
passed to a directive's attribute. Just as an example, say we have a
simple directive like this:
<div testme field="fieldval"></div>
.directive("testme", function () {
return {
template: "<div id='testme'></div>",
link: function (scope,elem, attrs){
$("#testme").html("Hello "+attrs.field);
}
}
})
The output is of course the value of the 'field' attribute:
Hello fieldval
However, what I want to do is create a new scope variable called
$scope.fieldval, where fieldval is actually the value passed into the
attribute 'field'
Any ideas?

No comments:

Post a Comment