Update Parent from Child 1
by Matthew Day
HTML
<div ng-app="myApp">
<div ng-controller="ParentCtrl">
A = {{data.a}}<br>
B = {{b}}
<div ng-controller="ChildCtrl">
<a ng-click="incrementA()">inc A</a> | <a ng-click="incrementB()">inc B</a>
</div>
</div>
</div>
JavaScript
angular.module('myApp', []);
angular.module('myApp')
.controller('ParentCtrl', function($scope) {
$scope.data = { a: 0 };
$scope.b = 0;
})
angular.module('myApp')
.controller('ChildCtrl', function($scope) {
$scope.incrementA = function() {
// it's through "data" obj, so we are chaning the value defined on parent scope
$scope.data.a++;
};
$scope.incrementB = function() {
// $scope.b++ would create "b" property on current scope
$scope.$parent.b++;
};
})
// SOURCES
// http://jsfiddle.net/yjVD9/1/
// https://groups.google.com/forum/#!msg/angular/LDNz_TQQiNE/ygYrSvdI0A0J