Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
    
The scope is just an object, you could just take the value and name a key on the scope like the input value. – Florian Aug 12 '13 at 13:32

Found the answer: pretty obvious really!!

scope[attrs.field] = "Some value";
share|improve this answer

Here is a plunker, according to my comment:

http://plnkr.co/edit/FVm1PR0KPImp7pajdCN3?p=preview

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.