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

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/

Sign up to request clarification or add additional context in comments.

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.