angularjs - Service making the $scope not accessible on the html -
can't access scope, example putting {{ pagec }} on html isn't working when remove blogpostservice controller works fine again.
var app = angular.module('blog', []); app.factory('blogpostservice', ['$http', function ($http) { this.getmoredata = function (pagecount) { return $http.get('/api/posts/' + pagecount); } }]); app.controller('maincontroller', ['$scope', 'blogpostservice', function ($scope, blogpostservice) { $scope.pagec = 1; $scope.posts = []; this.getmoredata = function (posts) { blogpostservice.getmoredata(pagec).success(function () { alert('got successfully!!!'); }).error(function () { alert('something went wrong!!!'); }); } }]);
because had wrong factory implementation, factory should return object. must have got error in console(please check).
app.factory('blogpostservice', ['$http', function ($http) { function getmoredata (pagecount) { return $http.get('/api/posts/' + pagecount); } return { getmoredata: getmoredata } } ]); or can convert factory service, there need bind data this(context) doing before.
app.service('blogpostservice', ['$http', function ($http) { this.getmoredata = function (pagecount) { return $http.get('/api/posts/' + pagecount); } }]); also don't use
.success/.erroron$httpcall, deprecated. instead use.then.
Comments
Post a Comment