1

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?

1
  • The scope is just an object, you could just take the value and name a key on the scope like the input value. Commented Aug 12, 2013 at 13:32

2 Answers 2

2

Found the answer: pretty obvious really!!

scope[attrs.field] = "Some value";
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a plunker, according to my comment:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.