我遇到了这个问题,我无法弄清楚如何解决这个问题。我有这个组件:
(function() {
'use strict';
// Usage:
//
// Creates:
//
myApp
.component('navbar', {
//template:'htmlTemplate',
templateUrl: 'app/components/navbar/navbar.partial.html',
controller: ControllerController,
bindings: {
progress: '<'
},
});
ControllerController.$inject = ['$scope','$rootScope','changeState'];
function ControllerController($scope,$rootScope,changeState) {
var $ctrl = this;
$scope.$on('state-changed',function(event,args){
console.log(args);
});
$ctrl.$onInit = function() { };
$ctrl.$onChanges = function (changesObj) { };
$ctrl.$onDestory = function() { };
}
})();
在$ transitions.onSuccess(ui-router 1.0 Beta)上触发事件'状态改变'。下面的代码:
var myApp = angular.module('myApp', ['ui.router']);
myApp.controller('appCtrl', ['$scope', '$state', 'formDataService', '$timeout', '$transitions','changeState','$transitions',
function ($scope, $state, formDataService, $timeout, $transitions,changeState,$transitions) {
changeState.go('1');
$scope.stateByFar = 1;
$scope.currentState = function(){
return $state.current.name;
};
$scope.updateStateByFar = function(){
if (parseInt($scope.currentState())>$scope.stateByFar)
$scope.stateByFar=parseInt($scope.currentState());
};
$transitions.onSuccess({to: '*'}, function(){
$scope.updateStateByFar();
console.log($scope.stateByFar);
$scope.$broadcast('state-changed',{currentState: $scope.currentState(),
stateByFar : $scope.stateByFar});
}
);
}]);
[编辑] 广播的实际工作。不能在第一个国家广播。当主模块运行时,第一条指令是:$ state.go('1');我无法检测到这第一个state.go。进一步state.go被聆听。
你不是从riitscope广播?对 ? – DMCISSOKHO