1

I have the name of my form in $scope.model variable and its adding dynamically. I wanted to change the value of the form field according to $scope.model.
For example

 $scope.model = 'form.field.text';
 $scope.[$scope.model]= 'new value';

But i it doesn't change the value of $scope.form.field.text. How can I do this? Here's my plunkr

ojus kulkarni
1,9073 gold badges27 silver badges42 bronze badges
asked Apr 24, 2016 at 11:24
1
  • I guess it referring wrong model. Commented Apr 24, 2016 at 11:29

1 Answer 1

1

In this case you need to use $parse service which is used by Angular internally for exactly this purpose:

function ctrl($scope, $parse) {
 $scope.form = {
 field: {
 text: ''
 }
 };
 $scope.form.field.text = 'Myvalue';
 $scope.model = 'form.field.text';
 $parse($scope.model).assign($scope, 'new value');
 console.log($scope.form.field.text);
}

Demo: http://jsfiddle.net/8hkbdq20/1/

answered Apr 24, 2016 at 11:30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.