Pregunta

I am trying to implement pagination for my Laravel 4 + angular.js 1.0.7 application by reading this article.

My code looks,

var app = angular.module('cmsPlus', ['ngResource']);

app.service( 'Post', [ '$resource', function( $resource ) {
   return $resource( 'posts/:id', { id: '@id'} );
}]);

function PostCtrl($scope, $http, Post) {
   $scope.init = function () {
     console.log(Post); // returns empty object
     $scope.posts = Post.query(); // Post.query is not a function
     // $scope.posts = Post.get(); // Post.get is not a function
   }
}

Why I am getting empty object with console.log(Post);? And why it shows Post.query is not a function error? How can I solve it?

Edit: Communicating between two factory services

Now I have two factory services,

app.factory('Data', function(){
        return {
            root_path: "<?php echo Request::root(); ?>/"
        };
});

app.factory( 'Post', [ '$resource', function( $resource ) {
   return $resource( 'posts/:id', { id: '@id'} );
}]);

How can I access Data.root_path in Post factory service?

¿Fue útil?

Solución

I think you want to use factory instead of service, i.e.:

app.factory( 'Post', [ '$resource', 'Data', function( $resource, Data ) {
   return $resource( Data.root_path + 'posts/:id', { id: '@id'} );
}]);

This link shows the difference:

http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top