컨트롤러가 getMyList 서비스 함수를 호출하지만 $ http 호출이 완료되는 데 시간이 걸리므로 myLists가 채워지지 않은 문제가 있습니다.
어떻게이 문제를 해결할 수 있습니까?
서비스
app.factory('myService', function($http) {
var myList = [];
$http.get("http://myService/json.data").then(function (resp) {
myLists = resp.data;
});
return {
getMyList: function (name) {
for (var i = 0; i < myLists.length; i++) {
if (myList[i].name == name) {
return myLists[i];
}
}
}
}
}
컨트롤러 :
여기 내 코드입니다
app.controller('MainCtrl', function(myService,$scope) {
var testName = "test"
myService.getMyList(testName).then(function(resp) {
// do something with resp data
});
});
일? 그들 중 누구? – Hitmands
라우터가 없습니다. 하지만 컨트롤러에서 캐시 된 myLists에서 목록을 반환하는 myService.getMyList를 호출해야합니다. – SimpleCoder