我遇到了這個問題,我無法弄清楚如何解決這個問題。我有這個組件:
(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