怎么样
$scope.$broadcast( "parentCtrlUpdateChild", args );
然后
在你的孩子身上
$scope.$on("parentCtrlUpdateChild", function(event, args) {
...
});
见 Todd Motto 的解释https://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/
看看我为你准备的这个垃圾桶
http://jsbin.com/zinuqikemo/edit?html,js,output
function parent() {
return {
restrict: 'E',
template: '<div><div><input type="text" ng-model="item"/></div><ng-transclude></ng-transclude></div>',
controllerAs: 'pctrl',
transclude: true,
controller: function($scope) {
$scope.item = 25;
$scope.hola = function() {
$scope.item = $scope.item + 1;
$scope.$broadcast('hola', $scope.item);
};
}
};
}
function child() {
return {
restrict: 'E',
scope: {
item: '='
},
template: '<div><div><input type="text" ng-model="item2"/><div></div></div></div>',
bindToController: true,
controllerAs: 'cctrl',
controller: function($scope) {
$scope.$on('hola', function (event, args) {
$scope.item2 = args + 25;
});
}
};
}
在你的 HTML 中添加这个
<button ng-click="hola()">HOLA</button>