In AngularJS, scopes use prototypical inheritance from their parents.
Prototypical inheritance basically means that JavaScript will look at the parent scope if it doesn't find a property on the child scope.
So, when you do $scope.person.name = 'Ari Lerner'
, JavaScript looks at $scope
, sees that it doesn't have a person
property, then goes to its parent (the parent scope), sees that it has a person
property, and assigns the name
property of that to 'Ari'
.
On the other hand, when you do $scope.person = { ... }
, JavaScript doesn't care if the property exists or not - it simply carries out the assignment, ensuring that your $scope
now has a person
property. The problem here is that your child scope's person
property now shadows the person
property of the parent scope, which still has its original value. So, in the parent controller, person
never changed.
For further reading, check this answer here:
What are the nuances of scope prototypal / prototypical inheritance in AngularJS?
You are creating a child scope that prototypically inherits from its parent scope. This is the case unless you are using an isolate scope (directive).
For a really great explanation, see here What are the nuances of scope prototypal / prototypical inheritance in AngularJS?
As others stated, this is because of your scopes.
For example, if you want to create a new object in your childScope, you do $scope.someObject = {};
.
Now, JavaScript can't know the difference between
$scope.someNewObject = {};
and
$scope.person = {};
because you are just assigning a new object to your childscope.
The other notation works, because by grabbing $scope.person.attribute = ...
JavaScript knows, that the person object already exists. It starts looking for this object in your childscope, can't find it there, goes to parentscope and finds it there and sets the attribute.
In conclusion you either have to use $scope.person.attribute
, or you use $scope.$parent.person = {};
I have a div that I need to hide when an option is selected in an option, below is my current html: Type:<select name="Type" id="Type"> <option value="choice">Multiple choice</...
I have a div that I need to hide when an option is selected in an option, below is my current html: Type:<select name="Type" id="Type"> <option value="choice">Multiple choice</...
So I want to swap two div elements that have a CKEditor inside. I viewed some of the previous questions and tried to do it that way. It's all OK, the elements are swapped. But one of the elements ...
So I want to swap two div elements that have a CKEditor inside. I viewed some of the previous questions and tried to do it that way. It's all OK, the elements are swapped. But one of the elements ...
Cache all requests from an app without explicitly specifying urlsToCache. So I will cache stuff under fetch event. To respond to requests from the cache. Update the cache when fetch is success. ...
Cache all requests from an app without explicitly specifying urlsToCache. So I will cache stuff under fetch event. To respond to requests from the cache. Update the cache when fetch is success. ...
I am C++ programmer and new to JS. I am trying to filter table based on click event. I have following table. How can i keep records based on clicking keys and filtering other records. <!DOCTYPE ...
I am C++ programmer and new to JS. I am trying to filter table based on click event. I have following table. How can i keep records based on clicking keys and filtering other records. <!DOCTYPE ...