2014-03-02 43 views
0

我可以看到RESTful服務這條路線被訪問之前內loadProfile被稱爲....如何將解析的數據注入控制器?

$routeProvider.when('/editProfile', { 
    templateUrl: '/views/user/editprofile', 
    controller: 'UserCtrl', 
    access: access.user, 
    resolve: { 
     userProfile: function(UserSvc){ 
      UserSvc.loadProfile(function(userProfile){ return userProfile }); 
     } 
    } 
}); 

那麼,如何再獲得這些數據注入到我的用戶控制器?

更新1

馬特方式避讓我如何獲得在我的控制器注入所解析數據。但是,我立即收到一個錯誤「Unknown Provider:userProfileProvider」。基於其他一些類似的問題,我決定嘗試從相關視圖中刪除ng-controller =「UserCtrl」屬性。對於我的/ editProfile路由,未知提供者錯誤消失了,但隨後開始出現在此控制器的所有其他視圖/路由上。

更新2

我看了egghead.io一對夫婦的視頻,發現我能夠訪問在$路徑所承諾的數據。添加到我的控制器,瞧!

if($route.current.locals.userProfile){ 
    $scope.userProfile = $route.current.locals.userProfile; 
} 

回答

0

只需將解析項目名稱作爲控制器功能的輸入。例如:

myApp.controller('UserCtrl', ['$scope', 'userProfile', 
function($scope, userProfile) { 
    // Controller code goes here 
}]); 
+0

有趣。我剛剛嘗試過,現在我收到一個錯誤...「Unknown provider:userProfileProvider」 –

+0

您的'routeProvider'配置與'UserCtrl'相同的模塊的一部分嗎? –

+0

是... angular.module( '對myApp',[ 'ngCookies', 'ngRoute', 'ngSanitize']) 的.config([ '$ routeProvider', '$ locationProvider', '$ httpProvider', 函數($ routeProvider,$ locationProvider,$ httpProvider){... var UserCtrl = angular.module('myApp')。controller('UserCtrl', ['$ rootScope','$ scope','$ $ root',$ $'','$ timeout','$ window','$ upload','UserSvc','AuthSvc','MediaSvc', function($ rootScope ,$ scope,$ sanitize,$ translate,$ location,$ interval,$ timeout,$ window,$ upload,UserSvc,AuthSvc,MediaSvc){ –

0

您能否返回加載的配置文件?我認爲這就是爲什麼你會遇到未知的提供者錯誤。

function (UserSvc) { 
    return UserSvc.loadProfile(function(userProfile) { return userProfile }); 
} 

作爲一個方面說明,我看到您的評論您在其中粘貼您的控制器功能,它看起來像有很大的依賴性。可能會更好地減少所需的協作者數量。