1
AngularJS的新手和我想我不明白如何從另一個工廠調用另一個Promise方法。每當我的代碼進入processPerson中的$ http.get時,我在IE中收到Function Expected錯誤,或者Chrome中的對象不是函數錯誤。我試過很多次重組代碼,多個工廠等,並且通常會得到相同的錯誤。我唯一能做到這一點的工作是,如果我在getPersonnel的成功範圍內組合了processPerson函數所嵌入的函數。AngularJS - 函數預期或對象不是函數
代碼:
(function(){
var app = angular.module('hrSite', ['personnel']);
app.controller('PersonnelController', function($scope, personnelFactory){
var personnelPromise = personnelFactory.getPersonnel();
personnelPromise.then(function(personnel){
var perDefs = new Array();
$.each(personnel.data.value, function(i, person){
var perDef = personnelFactory.processPerson(person);
perDefs.push(perDef);
});
$q.all(perDefs).then(function(){
$scope.personnel = personnel.data.value;
});
});
});
})();
(function(){
var personnelModule = angular.module('personnel', []);
personnelModule.factory('personnelFactory', function($http, $q) {
var getPersonnel = function(){
return $http.get("/sites/Development/_api/web/lists/getbytitle('Personnel')/items");
};
var processPerson = function(person){
var deferred = $q.defer();
$http.get("/sites/Development/_api/web/lists/getbytitle('Personnel Skills')/items?$select=*,Skill/Id,Skill/Title&$filter=PersonId eq '"+person.Id+"'&$expand=Skill").then(function(skills){
person.Skills = skills.data.value;
person.SkillsId = [];
$.each(skills.data.value, function(j, skill){
person.SkillsId.push(skill.Id);
});
deferred.resolve();
});
return deferred.promise();
};
return {getPersonnel: getPersonnel,
processPerson: processPerson}
});
})();
$ q未注入控制器。 – micronyks
micronyks是正確的,將控制器更改爲'app.controller('PersonnelController',function($ scope,personnelFactory,$ q){' –
雖然爲真,但並未解決錯誤。 (具體如下:\t \t \t \t \t \t $ http.get(「/ sites/Development/_api/web/lists/getbytitle('Personnel Skills')/ items?$ select = *,Skill/Id,Skill/Title&$ filter = PersonId eq'「+ person.Id +」'&$ expand = Skill「)。then(function(skills){ –