AngularJS監(jiān)聽路由的變化示例代碼
話不多說,我們下面直接來看實現(xiàn)的示例代碼
【一】Angular 路由狀態(tài)發(fā)生改變時可以通過' $stateChangeStart '、' $stateChangeSuccess '、' $stateChangeError '監(jiān)聽,通過注入'$location'實現(xiàn)狀態(tài)的管理
代碼示例如下:
function run($ionicPlatform, $location, Service, $rootScope, $stateParams) {
//路由監(jiān)聽事件
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams) {
console.log(event);
console.log(toState);
console.log(toParams);
console.log(fromState);
console.log(fromParams);
if (toState.name == "homePage") {
//獲取參數(shù)之后可以調(diào)請求判斷需要渲染什么頁面,渲染不同的頁面通過 $location 實現(xiàn)
if (toParams.id == 10) {
//$location.path();//獲取路由地址
// $location.path('/validation').replace();
// event.preventDefault()可以阻止模板解析
}
}
})
// stateChangeSuccess 當(dāng)模板解析完成后觸發(fā)
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
})
// $stateChangeError 當(dāng)模板解析過程中發(fā)生錯誤時觸發(fā)
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
})
}
【2】在頁面渲染中 可通過' $viewContentLoading '和 ' $viewContentLoaded '監(jiān)聽頁面渲染狀態(tài):渲染開始和渲染結(jié)束。
(在控制器中添加以下代碼實現(xiàn)監(jiān)聽)
// $viewContentLoading- 當(dāng)視圖開始加載,DOM渲染完成之前觸發(fā),該事件將在$scope鏈上廣播此事件。
scope.$watch('$viewContentLoading',function(event, viewConfig){
alert('模板加載完成前');
});
//$viewContentLoaded- 當(dāng)視圖加載完成,DOM渲染完成之后觸發(fā),視圖所在的$scope發(fā)出該事件。
$scope.$watch('$viewContentLoaded',function(event){
alert('模板加載完成后');
});
總結(jié)
以上就是這篇文章的全部內(nèi)容,希望能對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
angular4 JavaScript內(nèi)存溢出問題
本篇文章主要介紹了angular4 JavaScript內(nèi)存溢出問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
AngularJS內(nèi)建服務(wù)$location及其功能詳解
這篇文章主要為大家詳細介紹了AngularJS內(nèi)建服務(wù)$location及$location功能,感興趣的小伙伴們可以參考一下2016-07-07
AngularJS轉(zhuǎn)換響應(yīng)內(nèi)容
這篇文章主要介紹了AngularJS轉(zhuǎn)換響應(yīng)內(nèi)容 的相關(guān)資料,需要的朋友可以參考下2016-01-01

