How can I take variable changes and easily propagate them back to the ParentCtrl despite a 'new' var being instantiated in the ChildCtrl? Extra points for minimal to no $on's and $watch's (makes it easier to implement)
ParentCtrl
My ChildCtrl's are just different enough where I can't easily abstract a master layout and a ng-view, but they all depend on the same functions in ParentCtrl.
$scope.searchTerms is defined in ParentCtrl but the input box with ng-model='searchTerms' is in the view of the child controllers. When this var changes it's not reflected in the ParentCtrl only the ChildCtrls.
Example:
http://jsfiddle.net/JHwxP/22/
HTML Partial
<div ng-app>
<div ng-controller="Parent">
parentX = {{x}} <br/>
parentY = {{y}}<br/>
<div ng-controller="Child">
childX = {{x}}<br/>
childY = {{y}}<br/>
<a ng-click="modifyBothScopes()">modifyBothScopes</a><br>
<br>
The changes here need to appear in 'Parent'. <input ng-model='y'>
</div>
</div>
</div>
Controllers
function Parent($scope) {
$scope.x= 5;
$scope.y= 5;
}
function Child($scope) {
$scope.modifyBothScopes= function() {
$scope.$parent.x++;
};
}
UPDATE
I'm currently attempting a shared service approach: https://gist.github.com/exclsr/3595424
UPDATE
Trying an emit/broadcast system
SOLVED
Problem:
I was storing $scope.searchTerms in the parent and when changed created a space in the child $scope.
Solution:
I should have done $scope.search.terms in the parent and when changed in the child it would bubble up to the parent.
Example: http://jsfiddle.net/JHwxP/23/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…