我正在尝试在.factory中运行一个函数来获取帖子,但是我的控制器上出现了一个未定义的错误。
.factory('Portfolio', function($http, $log) {
//get jsonp
this.getPosts = function($scope) {
$http.jsonp("http://test.uxco.co/json-test.php?callback=JSON_CALLBACK")
.success(function(result) {
$scope.posts = $scope.posts.concat(result);
$scope.$broadcast("scroll.infiniteScrollComplete");
});
};
});
.controller('PortfolioCtrl', function($scope, Portfolio) {
$scope.posts = [];
Portfolio.getPosts($scope);
谢谢!
答案 0 :(得分:1)
你写的是service
而不是factory
。所以你要么
.service
而不是.factory
。.factory('Portfolio', function($http, $log) { //get jsonp return { getPosts: function($scope) { $http.jsonp() .success(function(result) { $scope.posts = $scope.posts.concat(result); $scope.$broadcast("scroll.infiniteScrollComplete"); }); }; } });
答案 1 :(得分:0)
使用像这样的.factory代码
.factory('Portfolio', function($http, $log) {
//get jsonp
return{
getPosts: function($scope) {
$http.jsonp("http://test.uxco.co/json-test.php?callback=JSON_CALLBACK")
.success(function(result) {
$scope.posts = $scope.posts.concat(result);
$scope.$broadcast("scroll.infiniteScrollComplete");
});
};
}
});