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/.error on $http call, deprecated. instead use .then.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -