Maison > Questions et réponses > le corps du texte
类似于这样的情况
<p ng-controller="parentCtr">
<p ng-controller="childCtr1">
<p ng-controller="childCtr2">
</p>
</p>
</p>
这些控制器都是在一个module里,我需要的是把parentCtr里的变量传给childCtr2
然后在childCtr2中通过$on获取
迷茫2017-04-10 17:53:49
<p ng-controller="ParentCtrl">
<button ng-click="click()">change</button>
<p ng-controller="ChildCtrl1">
<p ng-controller="ChildCtrl2">
</p>
</p>
</p>
angular.module('myApp', [])
.controller('ParentCtrl', function ($scope) {
$scope.click = function () {
$scope.$broadcast('change', {name: 'xxx'});
}
})
.controller('ChildCtrl1', function ($scope) {
})
.controller('ChildCtrl2', function ($scope) {
$scope.$on('change', function (event, obj) {
console.log(obj);
});
});
Scope是继承的,如果只是单纯想获取父Scope的数据,这样就可以了
angular.module('myApp', [])
.controller('ParentCtrl', function ($scope) {
$scope.data=[1,2,3];
})
.controller('ChildCtrl1', function ($scope) {
})
.controller('ChildCtrl2', function ($scope) {
console.log($scope.data);
});