我需要父包装器controller
来始终运行并将数据推送到任何子容器。当父包装器上的事件更改属性的范围时,我需要孩子controllers
更新
secure.controller('wrapperCtrl', function ($scope, storeFactory, safeFactory) {
//get first store
storeFactory.setStoreSummary(1).then(function (storeData) {
$scope.store = storeData
safeFactory.setSafe(storeData.safes[0].id).then(function(safeData){
$scope.safe = safeData;
});
});
$scope.changeSafe = function (safeId) {
safeFactory.setSafe(safeId).then(function (safeData) {
$scope.safe = safeData;
});
}
});
在index.html
页面上,我有以下内容:
<div class="pageTitle">
<h1>{{title}}</h1>
<h2>{{store.storeName}}</h2>
<ul class="safes">
<li ng-repeat="safe in store.safes"><a href="#_" ng-class="isActive(safe.id)" ng-click="changeSafe(safe.id)" ng-cloak>{{safe.name}}</a></li>
</ul>
</div>
<div ng-view class="fade"></div>
您可以看到ng-view
加载子controllers
。我是否可以在子控制器中查看$scope.changeSafe
运行中的wrapperCtrl
函数并重新绑定它$scope.safe
答案 0 :(得分:1)
如果您需要更明确的答案:
secure.controller('wrapperCtrl', function ($scope, storeFactory, safeFactory) {
//get first store
...
$scope.changeSafe = function (safeId) {
safeFactory.setSafe(safeId).then(function (safeData) {
$scope.safe = safeData;
$scope.$broadcast('changeSafely'); // broadcast changeSafely event in parent ctrl
});
}
});
secure.controller('childCtrl', function($scope, ...) {
function doSomething(){
...
}
$scope.$on('changeSafely', doSomething); // capture event and do you stuff
});