2016-02-01 29 views
0

我正在處理個人檔案頁面中有封面的社交應用程序。 「編輯」和「訪問」頁面都有封面,但有不同的子部分。 問題是,封面上顯示的一些控制器嚴格依賴於您當前訪問的部分。UI-Router:我可以在子狀態下解析父承諾嗎?

要解決此問題,我試圖返回父狀態的解決部分中的承諾,然後解析不同子狀態中的承諾。

.state('profile', { 
    url: '/profile', 
    controller: 'ProfileController', 
    templateUrl: 'components/profile/profile.html', 
    resolve: { 
     actor: function(UserService){ 
      return UserService.getUser(); 
     }, 
     target: function($q){ 
      var deferred = $q.defer(); 
      return deferred.promise; 
     } 
    } 
}) 
.state('profile.view', { 
    url: '/:userId', 
    templateUrl: 'components/profile/view.html', 
    navbar: true, 
    parent: 'profile', 
    resolve: { 
     /** 
     * 
     * @param target 
     * @param {UserService} UserService 
     */ 
     target: function(target, UserService){ 
      target.resolve(UserService.getActions('view')); 
     } 
    } 
}) 

不幸的是,有一些錯誤的,我的邏輯,因爲配置文件的狀態裏面return deferred.promise阻止該應用程序。

誰能幫我弄清楚我做錯了什麼嗎?

最好!

回答

0

返回承諾對分解功能

.state('profile.view', { 
    url: '/:userId', 
    templateUrl: 'components/profile/view.html', 
    navbar: true, 
    parent: 'profile', 
    resolve: { 
     /** 
     * 
     * @param target 
     * @param {UserService} UserService 
     */ 
     target: function(target, UserService){ 
      return UserService.getActions('view').$promise; 
     } 
    } 
}) 

這個回答假設UserService$resource查詢。其他API返回承諾的方式不同,但原理相同。 返回對解析器功能的承諾。

+0

我是否也必須從'profile'狀態中刪除承諾? –