angularjs - Angular $资源设置: Communicating between factory services

标签 angularjs laravel-4 angularjs-directive angular-resource angular-services

我正在尝试通过阅读 this article 为我的 Laravel 4 + angular.js 1.0.7 应用程序实现分页.

我的代码看起来,

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
   }
}

为什么我使用 console.log(Post); 得到空对象?为什么它显示 Post.query is not a function 错误?怎么解决呢?

编辑:两个工厂服务之间的通信

现在我有两个工厂服务,

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

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

如何访问 Post 工厂服务中的 Data.root_path

最佳答案

我认为你想使用工厂而不是服务,即:

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

此链接显示了差异:

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

关于angularjs - Angular $资源设置: Communicating between factory services,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19667405/

相关文章:

javascript - AngularJs - 在指令中使用带有 ng-bind 的过滤器

javascript - 如何检测焦点何时超出 Div?

angularjs - 调用超过 1 个 $rootScope.$on

mysql - 最爱笑话的餐 table 星座

javascript - 当属性更改时,AngularJS 绑定(bind)不起作用

javascript - ng-repeat 不能作为参数传递

AngularJS ng-switch渲染顺序

jsf - 我们可以在 Facelets 页面中包含 angularjs 组件吗

php - 找不到 Controller 方法 - laravel 4

unicode - Str::slug 替代印地语和阿拉伯语字符串?