0

HTML

<input ng-model="test1.param">
<input ng-model="test2">

Controller

$scope.test1 = { param: null };
$scope.test2 = null;

After entering some text in both inputs:

  • $scope.test1.param becomes entered text.
  • $scope.test2 remains null.

Why $scope.test2 value does not change?

2 Answers 2

1

"If you use ng-model, you have to have a dot in there." Make your model point to an object.property and you'll be good to go.

This happens when child scopes are in play - like child routes or ng-repeats. The child-scope creates it's own value and a name conflict is born.

Better explanation you have here: https://stackoverflow.com/a/22768720/1081079

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

Comments

0

It happens when you are in a child scope. A parent object is known in the child scope but simple variable is recreate for the child. so when you use $scope.test1 it takes the parent variable, but when you use $scope.test2 it create new variable to the child so it is not change the parents one. to solve this either change $scope.test2 to be also object as $scope.test1 $scope.test2 ={ param: null} or use the $parent service:

  <input ng-model="$parent.test2">

Comments

Your Answer

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.