2013-04-13 66 views
1

我想讓我的angularFire集合解決路由加載。例如:angularFire路由解析

App.config ($routeProvider, angularFireProvider) -> 
    $routeProvider.when '/test', 
    templateUrl: 'views/test.html' 
    controller: 'TestCtrl' 
    resolve: angularFireProvider.resolve 'testItems' 

是否可以這樣做?

回答

2

我不完全確定你爲什麼希望集合在路由加載時解決,而不是在控制器中 - 你能否詳細說明一下?例如,下面將工作太:

App.config ($routeProvider, angularFireProvider) -> 
    $routeProvider.when '/test', 
    controller: 'TestCtrl' 

function TestCtrl($scope, angularFire) { 
    angularFire("https://<example>.firebaseio.com", $scope, "collection"). 
    then(function() { 
     // The collection has been resolved and available in $scope.collection 
    }); 
} 

是它主要的問題語法上的方便還是我失去了你在上面要的功能?

更新:

App.config(['$routeProvider', 'angularFire', function($routeProvider, angularFire) { 
    $routeProvider.when("/test", { 
    templateUrl: 'views/test.html' 
    controller: 'TestCtrl', 
    resolve: {collection: angularFire("https://<example>.firebaseio.com")} 
    }); 
}]); 

function TestCtrl(collection) { 
    // collection has already been resolved to its value. 
} 
+0

在這個方法'$ routeChangeSuccess'將要發射的'angularFire'資源得到解決之前,右:對於前$routeChangeSuccess事件被激發到解決的價值? –

+0

你是對的 - 如果你想在事件觸發之前解決這個問題,你可以簡單地將調用轉到'angularFire'到你的解析屬性中,因爲它返回一個promise。我將編輯答案以提供代碼片段。 – Anant

+0

Anant,您可能會看到這一點的機會,我試圖使用解析功能,但它指出angularFire是一個未知的提供者(在相關的說明是angularFireCollection不適合在這種情況下由於不需要範圍? )。 aF和aFC在我的代碼的其他地方工作正常,但是有可能是我在配置和解決使用它時丟失的東西? – Michael