Here are two snippets of code:
<input type="text" ng-model="data.message">
<div>Hello, {{data.message}}</div>
<div ng-controller="firstCtrl">
<input type="text" ng-model="data.message">
<div>Hello, {{data.message}}</div>
</div>
<div ng-controller="secondCtrl">
<input type="text" ng-model="data.message">
<div>Hello, {{data.message}}</div>
</div>
and
<input type="text" ng-model="msg">
<div>Hello, {{msg}}</div>
<div ng-controller="firstCtrl">
<input type="text" ng-model="msg">
<div>Hello, {{msg}}</div>
</div>
<div ng-controller="secondCtrl">
<input type="text" ng-model="msg">
<div>Hello, {{msg}}</div>
</div>
ng-controller
here creates new scope so the firstCtrl
and secondCtrl
scope prototypically inherits from root scope in both cases. So, ideally, when the property of a children is overwritten is shadows
the inherited value from parent and the value in parent stays same. Then why do the two snippets work differently?
Also, why in the first snippet, changing the value in firstCtrl
changes the value in root scope as well?