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
-
I guess it referring wrong model.john51– john512016年04月24日 11:29:02 +00:00Commented Apr 24, 2016 at 11:29
1 Answer 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);
}
answered Apr 24, 2016 at 11:30
dfsq
193k26 gold badges244 silver badges261 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default