[英]Angular ui-router: onEnter function is not firing when new state is entered
[英]Angular ui-router onEnter function not firing
感謝SO社區的一些幫助,我有一個工作角度SPA,使用ui-router庫使用多個狀態/視圖。 但是我得到的行為似乎與angular-ui-router
文檔不一致。
從他們關於onEnter的文檔中,我希望無論何時我使用$state.go("home")
,與該狀態的配置對象相關聯的onEnter()
事件都會啟動,但我不認為它會發生。 例:
/* myApp module */
var myApp = angular.module('myApp', ['ui.router'])
.config(['$stateProvider', function ($stateProvider) {
$stateProvider.state('home', {
url: "/",
views: {
'top': {
url: "",
template: '<div>top! {{topmsg}}</div>',
controller: function ($scope) {
$scope.topmsg = "loaded top!";
console.log("top ctrl!");
},
onEnter: function () {
console.log("entered bottom state's onEnter function");
}
},
'middle': {
url: "",
template: '<div>middle! {{middlemsg}}</div>',
controller: function ($scope) {
$scope.middlemsg = "loaded middle!";
console.log("middle ctrl!");
},
onEnter: function () {
console.log("entered bottom state's onEnter function");
}
},
'bottom': {
url: "",
template: '<div>bottom! {{bottommsg}}</div>',
controller: function ($scope) {
$scope.bottommsg = "loaded bottom!";
console.log("bottom ctrl!");
},
onEnter: function () {
console.log("entered bottom state's onEnter function");
}
}
},
onEnter: function () {
console.log("entered home state");
}
});
}])
.controller('MyAppCtrl', function ($scope, $state/*, $stateParams*/) {
$scope.statename = $state.current.name;
$scope.testmsg = "app scope working!";
console.log("MyAppCtrl initialized!");
$state.go("home");
});
正如你可以從狀態的配置對象中的所有onEnter()
調用中看到的onEnter()
,我的期望是當家庭狀態加載時,我會開始看到他們正在觸發控制台消息的所有狀態/視圖的列表。 但是,僅從home
狀態的onEnter
事件中記錄第一個entered home state
消息。 沒有一個其他視圖的onEnter
被擊中。 我誤讀了文檔嗎?
這是一個期待已久的小提琴演示。 只需在firebug / chrome devtools中顯示控制台,您就會看到缺少控制台輸出。
任何幫助都會很棒。 我正在使用角度1.2和ui-router 0.2.0。 提前致謝!
兩件事情:
onEnter
方法適用於狀態 ,而不適用於視圖 。 您在定義中有一個狀態和三個視圖,並且由於某種原因,您嘗試將URL附加到您的視圖。
為了輸入一個州,首先必須退出。 (A)你實際上從未離開過home
狀態(因為你只有一個狀態定義開始),所以你永遠不能重新輸入它。 (B)看起來你實際上是想在home
創造兒童狀態。 即使您修復了語法,它仍然無法工作,因為當您輸入父項的子狀態時,通過更改為同一父項中的其他子狀態,您仍然永遠不會實際離開父狀態,因此不會重新觸發onEnter
。
我用了
$state.reload();
在我的案例中觸發承諾響應的更新來解決這個問題。 你也可以
$state.reload()
$state.go("statename")
這就是我解決它的方式。
請看: https : //github.com/angular-ui/ui-router/wiki/Quick-Reference#statereload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.