我有一个角度服务,使用该服务对控制器可见一个功能。即。的getData
我希望我的服务能够在服务中使用名为modifyData的辅助函数,该函数在将数据返回到服务之前处理数据,然后服务将数据发送到控制器。我不想在服务之外使用帮助函数,也不想在服务之外访问它。
app.service("dataService", function() {
/* HELPER FUNCTION */
var modifyData = function(data) {
data.modified = true;
return data;
}
this.getData = function() {
//Do a http request to get oldData variable
var newData = modifyData(oldData);
return newData;
}});
Angular的错误是错误:modifyData未定义。
我在这里做错了什么?
编辑:这是我的实际代码,因为我的示例代码应该可以工作,然后我可能已经将它简化了太多。
rabApp.service("reviewService",
["$http", "$q", "beerService", "userService",
function($http, $q, beerService, userService) {
/* HELPER FUNCTIONS */
/* Builds a complete review object from a user object, beer object, and partial review object */
var buildReviewObj = function(reviewObj, switchClass) {
var deferred = $q.defer();
if(switchClass) {
reviewObj.dirClass = "left";
} else {
reviewObj.dirClass = "right";
}
//Make sure the review picture is valid and if not use default one
if(reviewObj.image.length <= 0) {
reviewObj.image = "images/default-beer-pic.jpg";
}
//Make sure the review style is present if not put a default in
if(reviewObj.style.length <= 0) {
reviewObj.style = "Unknown";
}
//Add user data
userService.getUser(reviewObj.author_id).success(function(data, status) {
//Check if we have a matched user
if(data.status === "success") {
var userObj = data.data;
//Make sure the profile picture is valid and if not use default one
if(userObj.profile_pic.length <= 0) {
userObj.profile_pic = "images/default-profile-pic.jpg";
}
reviewObj.author = userObj;
} else {
deferred.reject("Bad User object returned");
}
});
//Add beer data
beerService.getBeer(reviewObj.beer_id).success(function(data, status) {
//Check if we have a matched beer
if(data.status === "success") {
var beerObj = data.data;
reviewObj.beer = beerObj;
} else {
deferred.reject("Bad Beer object returned");
}
});
deferred.resolve(reviewObj);
}
/* Gets an array of review objects from the backend
* @return Array of Review objects
*/
this.getReviews = function() {
var deferred = $q.defer();
$http({
method: "GET",
url: "/includes/services/reviews.php",
params: { a : "getReviews",
limit : "10" }
}).success(function(data, status) {
//switchClass variable alternates the review to display left or right
var switchClass = true;
//Notify review controller that we are loading reviews
deferred.notify("loading");
//Check if we have reviews
if(data.status === "success") {
var reviews = [];
//Add beer and user data to each review
data.data.forEach(function(reviewObj, index) {
buildReviewObj(reviewObj, switchClass).then(function(data) {
switchClass = !switchClass;
reviews.push(reviewObj);
});
});
//Notify review controller that we are done
deferred.notify("finished");
//Review array built successfully return reviews
deferred.resolve(reviews);
} else {
//Couldn't get reviews return false
deferred.reject("Couldn't access reviews.php back end service");
}
}).error(function() {
//Couldn't get reviews return false
deferred.reject("Couldn't access reviews.php back end service");
});
return deferred.promise;
}}]);
答案 0 :(得分:2)
我发现了我错过的错误
return deferred.promise;
在辅助函数.... facepalm 这个帖子在我发布后花了10分钟才解决,但只是在我发布这里之前我挣扎了一个半小时。