2017-01-27 262 views
0

在我的角度樣的路線,我試圖做的事:解決角UI路由器

(function(angular){ 
    'use strict'; 

    angular 
     .module('appRouter',['ui.router']) 
     .controller('routerCtrl',function(myResolveServ,resolveData){ 
      console.log(resolveData); 

     }) 
     .service('myResolveServ',function($http){ 
      this.getResolveData = function($http){ 
       var root = 'https://jsonplaceholder.typicode.com'; 
       return $http.get(root+'/posts/1').then(function(response){ 

        return response.data; 
       }); 
      } 
     }) 
     .config(function($stateProvider,$urlRouterProvider){ 
      $stateProvider 
       .state('settings.profile',{ 
        url:'/profile', 
        templateUrl:'templates/profile.html' 
       }) 
       .state('settings.account',{ 
        url:'/account', 
        templateUrl:'templates/account.html', 
        controller:'routerCtrl', 
        controllerAs:'vm', 
        resolve:{ 
         resolveData :function(myResolveServ){ 
          var data = myResolveServ.getResolveData(); 
         } 
        } 
       }); 

      $urlRouterProvider.otherwise('/settings/profile'); 


     }); 

})(window.angular); 

但這代碼不起作用。我無法導航到帳戶頁面URL /settings/account

錯誤在控制檯中不明顯。

我在做什麼錯?

UPDATE

我知道這個工程:

   resolve:{ 
        resolveData : function($http){ 
         var root = 'https://jsonplaceholder.typicode.com'; 
         return $http.get(root+'/posts/1').then(function(response){ 

          return response.data; 
         }); 
        } 
       } 
+0

您說'去URL /設置/帳戶' - 那究竟是什麼? settings.account是狀態,它的URL是/ account,所以哪一個是/ settings/account? – rrd

回答

0

你需要在你的決心返回數據路由settings.account

resolveData: function(myResolveServ) { 
    return myResolveServ.getResolveData(); 
} 
+0

我試過了,但它沒有記錄在控制器中發生的控制檯中。 – StrugglingCoder

+0

嘗試使用更新的代碼 – Dario

+0

不能正常工作。服務看起來好嗎?控制器應該記錄數據嗎? – StrugglingCoder

0

這很難說爲什麼,你應該看ui路由器錯誤。 粘貼下面的代碼,並尋找錯誤

var $rootScope = angular.element(document.querySelectorAll("#home")[0]).injector().get('$rootScope'); 

$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){ 
    console.log('$stateChangeStart to '+toState.to+'- fired when the transition begins. toState,toParams : \n',toState, toParams); 
}); 

$rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams){ 
    console.log('$stateChangeError - fired when an error occurs during transition.'); 
    console.log(arguments); 
}); 

$rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){ 
    console.log('$stateChangeSuccess to '+toState.name+'- fired once the state transition is complete.'); 
}); 

$rootScope.$on('$viewContentLoaded',function(event){ 
    console.log('$viewContentLoaded - fired after dom rendered',event); 
}); 

$rootScope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){ 
console.log('$stateNotFound '+unfoundState.to+' - fired when a state cannot be found by its name.'); 
    console.log(unfoundState, fromState, fromParams); 
});`