monkeyWithAMachinegun
monkeyWithAMachinegun

Reputation: 331

In a directive is it possible to target a variable on the scope using a string value?

I'm working on a directive and I need to set a variable on the scope, but all I have to work with is a string value that describes the path of the var:

var toChange = "targetObj['targetProp']";

I asked a question long ago regarding finding nested properties within a known object (and got a working answer), but haven't been successful leveraging that to look on the scope...is there a way to basically take that path and find it on the scope? This doesn't work but something along the lines of:

ERRONEOUS

scope.toChange = newValue;

Upvotes: 0

Views: 47

Answers (2)

georgeawg
georgeawg

Reputation: 48968

Another approach is to use $scope.$eval:

var toChange = "targetObj['targetProp']";

var target = $scope.$eval(toChange);

target = newVal;

For more information, see

Upvotes: 2

monkeyWithAMachinegun
monkeyWithAMachinegun

Reputation: 331

Actually I figured it out, sorry everyone!

var x = $parse(toChange);
x.assign(scope, newVal);

Upvotes: 1

Related Questions