我有類似的工廠與$ HTTP調用裏面像以下:
appModule = angular.module('appModule', []);
appModule.factory('Search', function($rootScope, $http) {
var Search;
Search = {};
Search.types: ["bacon"];
Search.pastEvents = null;
$http.get('api/highlights').success(function(response) {
return Search.pastEvents = response.data;
});
return Search;
});
var notes_module = angular.module('notes', []);
notes_module.config(['$routeProvider', function ($routeProvider) {
var notes_promise = ['Notes', '$route', 'Search', function (Notes, $route, Search) {
//suspect that Search not having pastEvents ready in time of calling index method
//Notes resource
return Notes.index({subject_id: 1 }, Search);
}];
$routeProvider.when('/notes', {
templateUrl:'index.tpl.html',
controller:'NotesCtrl',
resolve:{
notes: notes_promise,
}
});
}]);
我應該關心的就是當數據從$ HTTP調用已準備就緒,當這家工廠被初始化/注射? pastEvents會準備好嗎?如果我應該關心我該怎麼辦?
我懷疑搜索對象沒有準備好及時調用Notes資源的索引方法。
您能更具體些嗎?究竟「行爲奇怪」是什麼意思? –
如果您在呈現視圖之前關注數據可用(以避免閃爍),請檢查此[問題(和答案)](http://stackoverflow.com/questions/11972026/delaying-angularjs-route-change-until-模型加載到防止閃爍)。 – bmleite