在Angular.js控制器之间实现资源共享 [英] Sharing resources between controllers in Angular.js
本文介绍了在Angular.js控制器之间实现资源共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简单的问题。
我有这个资源:
var Company = $resource("/company/:_id", {_id: "@_id"});
这是我想不同的控制器之间共享。
That I want to share between different controllers.
现在,我复制粘贴周围的事物,但我仍然没有的地步,我想添加更多code和使用<一个href=\"http://stackoverflow.com/questions/12772255/angularjs-using-shared-servicewith-resource-to-share-data-between-controller\">angular共享服务
Right now, I am copy-pasting things around but I am still not at the point where I want to add more code and use angular shared services
任何其他的选择吗?
推荐答案
只要把服务或工厂。
angular.module("myApp", []).
factory("CompanyResource", function ($resource) {
return $resource("/company/:_id", {_id: "@_id"});
});
,然后你可以在控制器使用它
and then you can use it in the controller with
function MapCtrl($scope, $resource, $location, CompanyResource) {
...
CompanyResource.query();
...
}
请注意,你并不需要在工厂名称前的$符号。
Note that you do not need the $ sign in front of the factory name.
这篇关于在Angular.js控制器之间实现资源共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文