How to assign value in angular $ parse

I have a link bar to one of my scope values, like this:

var reference_string = "form.name"; 

And I want to assign a value to the object it refers to:

 $scope.form.name = 'newvalue'; 

Looking around, I found 2 possible solutions: using simple JS or using angular $ parse .

However, the $ parse function seems to return a value. Can I do this to assign a new value?

T. I want to do something like

 var reference_string = "form.name"; var reference = getReference($scope, reference_string); // ideally using an angular in-built function like $parse reference = 'newvalue'; // should have the same effect as $scope.form.name = 'newvalue'; 
+6
source share
2 answers

The object returned by $parse has an assign() method for setting values.

 var getter = $parse(reference_string); getter.assign($scope, 'newValue'); 

Plunker demo ~ http://plnkr.co/edit/RlhXRpJvQ69ZdEkstyq8?p=preview

+23
source

$ parse is an Angular service that converts an expression into a function. You can then call the function and pass in a context (usually a scope) to get the value of the expression. In addition, if an expression is assigned, the returned function will have the assign property. The assign property is a function that can be used to change the value of an expression in a given context.

enter the link here

-1
source

All Articles