gpt4 book ai didi

javascript - AngularJS 1.5 中的组件不监听来自 $scope 的广播事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:56:39 25 4
gpt4 key购买 nike

我遇到了这个问题,我真的不知道如何解决这个问题。我有这个组件:

    (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 () { };
}
})();

“state-changed”事件在 $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 被监听。

最佳答案

我不确定您是否遇到与我类似的问题,在这种情况下,我希望我的发现对您有所帮助。我的问题的完整描述和我找到的解决方案是 here .

简而言之,我在使用 $transitions 时遇到了一些问题,我发现,对于版本 1.0.0.beta1,tofrom 参数不工作。

所以,而不是

$transitions.onSuccess({to: '*', form: '*'}, function(){});

我正在使用

$transitions.onSuccess({}, function(){
if($state.current.name == 'myState') // do stuff
});

关于javascript - AngularJS 1.5 中的组件不监听来自 $scope 的广播事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com