[英]Angular service on more than one page
I'm doing an API dashboard and I need to split the Login Page from the rest of the application, because of template reasons. 我正在做一个API仪表板,由于模板原因,我需要将登录页面与应用程序的其余部分分开。
I created a service using a factory, like this: 我使用工厂创建了服务,如下所示:
angular.module('dashboard').factory('$api', function() {
var api_url = 'https://my.great.app/api/';
return {
resolve:function(path){
return api_url + '/' + path;
}
};
});
How can I share this with some other? 如何与其他人分享?
I'm a real new be on angular, I'm a little bit confused about using bower, grunt, nodes js and so on... 我是一个真正的新手,我对使用Bower,Grunt,Node js等感到有些困惑...
D. D.
Angular have good mechanism for dependency injection . Angular具有良好的依赖注入机制 。
In your case you can inject factory to controller with next code: 在您的情况下,您可以使用以下代码将工厂注入控制器:
angular.module('dashboard')
.controller('myCtrl', ['$scope', '$api', function($scope, $api){
var result = $api.resolve('path');
}])
a tip: 小费:
create a js file to create your module and share it for other files, example: 创建一个js文件来创建您的模块并与其他文件共享,例如:
main.js main.js
var App = angular.module("myModuelName", [])
so, now you can include this file in your HTML, this will share your App var to others files 因此,现在您可以将此文件包含在HTML中,这会将您的App变量共享给其他文件
so you will be able to do 这样你就可以做
App.controller("myCtrl" ['$scope', '$api', function($scope, $api){
$scope.path = $api.resolve('myInternalPath')
}])
Services are a good way to share information between controllers 服务是在控制器之间共享信息的好方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.