尝试使用 Angular $broadcast
,当我测试它并使用字符串“hi”,并在兄弟控制器中观察它时,它工作正常,如下所示:
//first controller
app.controller('colourKeyCtrl', colourKeyCtrl);
function colourKeyCtrl($scope, $timeout, patents, patentPhasesService) {
vm.$onInit = function() {
$scope.$broadcast('hi');
}
}
//second controller
app.controller('graphDonutCtrl', graphDonutCtrl);
function graphDonutCtrl($scope, patents, patentPhasesService, $timeout) {
$scope.$on('hi', function(event, opt){
alert('hello there')
})
}
一旦我将字符串更改为其他任何内容,例如phaseChange
,它就无法调用$on
第二个控制器中的方法。不知道为什么。我试图包装$broadcast
一个$timeout
方法,但这并没有解决问题。
问题
我是否$broadcast
以错误的方式使用或我的语法不正确?
.state('dashboard', {
url: '/dashboard',
views: {
'@': {
templateUrl: 'p3sweb/app/components/dashboard/views/dashboard.htm',
controller: 'dashboardCtrl',
controllerAs: '$ctrl'
},
'colourkeywidget@dashboard': {
templateUrl: 'p3sweb/app/components/dashboard/views/ui-views/colour-key-widget.htm',
controller: 'colourKeyCtrl',
controllerAs: '$ctrl'
},
'graphdonutwidget@dashboard': {
controller: 'graphDonutCtrl',
controllerAs: '$ctrl',
templateUrl: 'p3sweb/app/components/dashboard/views/ui-views/graph-donut-widget.htm',
}
}
})