我正在尝试通过阅读 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/