CroMagnon
CroMagnon

Reputation: 1208

Different Angular scope variabes behaviour

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:

Why $scope.test2 value does not change?

Upvotes: 0

Views: 51

Answers (2)

user4858836
user4858836

Reputation: 47

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">

Upvotes: 0

freshbm
freshbm

Reputation: 5622

"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

Upvotes: 1

Related Questions