我有一個JSON集我已上傳到我的火力點,網站和工具,通過位於前那點鍵在後者AngularFire中的子承諾 - 通過ID選擇相關的孩子?
"sites": {
"s001": {
"name": "ACT-105",
"description": "Intro Accounting",
"type": "course",
"thumbnail": "debate",
"toolCount": 4,
"tools" : ["t001","t002","t003"]
},
"s002": {
"name": "ART-201",
"description": "Pottery Lab",
"type": "course",
"thumbnail": "sculpture",
"toolCount": 4,
"tools" : ["t001","t002","t003","t004"]
},
"tools": {
"t001": {
"name": "main-tool",
"title": "Home",
"description": "Main tool",
"thumbnail": "home.jpeg"
},
"t002": {
"name": "announce-tool",
"title": "Announcements",
"description": "System Announcements",
"thumbnail": "announcements.jpeg"
},
我打開一個網址,並承諾陣列有關;然後抓取數組中的當前網站及其相關工具的數組,然後打開另一個承諾來循環並獲取所有相關工具。從警報中,它似乎只抓住一個工具然後退出。
angular.module("foo", ["firebase"]).
controller("MyCtrl", ["$scope", "angularFire", function($scope, angularFire) {
var dbRef = "https://sampledb.firebaseio.com";
var siteRef = new Firebase(dbRef + "/sites/s003");
var promise = angularFire(siteRef, $scope, "site", {});
var sitetools = [];
promise.then(function() {
sitetools = $scope.site.tools;
alert("tools " + sitetools);
}).then(function() {
var toolList = [];
for (var i=0;i<sitetools.length;i++)
{
alert("tool " + sitetools[i]);
toolList.push(getTool(dbRef,toolId));
}
$scope.tools = toolList;
});
}]);
var getTool = function(dbRef,toolId) {
var toolitem;
var toolRef = new Firebase(dbRef + "/tools/" + toolId);
alert(toolRef);
var promise = angularFire(toolRef, $scope, "tool", {});
promise.then(function() {
alert("found tool " + toolId);
toolitem = $scope.tool;
});
return toolitem;
};
的小提琴是在這裏:http://jsfiddle.net/5n9mj/1/
感謝最後一個提示與scope.broadcast - 我沒有真正理解你的例子,直到我讀了Angular文檔:http://docs.angularjs.org/api/ng.$rootScope.Scope –
另外,我看到你已經包括$超時但它沒有被使用/進一步定義;該功能的文檔不太清楚;是否包含了您的angularFire調用自動使用的$ timeout?或者是否將它作爲稍後指定的佔位符? –
@PatrickHaggood我在jsfiddle示例中使用了超時:http://jsfiddle.net/oburakevych/5n9mj/10/。 angularFire不需要$超時,但我注意到 - 當新項目添加到工具時,不會觸發摘要:$ scope.tools.push(snapshot.val());因此,我必須在$ push()之後放置$ scope。$ apply(),或者將其包裝在$ timeout中,這會觸發$自動應用。這可能是Firebase傢伙的問題 - 這似乎是一個錯誤。 –