如何创建与在AngularJS控制器之间可共享特性的对象吗? [英] How to create a object with properties that are sharable among controllers in AngularJS?
问题描述
这是一个后续问题<一href=\"http://stackoverflow.com/questions/22707699/how-to-create-this-global-constant-to-be-shared-among-controllers-in-angularjs\">How创造Angularjs控制器之间共享这个全局常量?
提供的答案让控制器之间共享一个常数$根目录。
应用程序= angular.module('对myApp',[]);
app.constant('$根目录','本地主机/根目录/应用');app.controller('myController的',['$范围,$根目录',函数($范围,Webroot公司$){
$ scope.webroot = $根目录;
}]);
然而,问题是,如果我有10常数,那么所有的10常数都被注入到控制器。这使得控制器声明看起来长和丑陋。 如何创建与在AngularJS控制器之间可共享属性的对象?就这样,我只需要注入大量的常量的一个对象来代替。可这Angularjs办呢?谢谢你。
VAR应用= angular.module('应用',[]);app.constant('配置',{
为prop1:VAL1',
prop2:'val2的,
...
});app.controller(按Ctrl',['配置',函数(配置){
的console.log(config.prop1);
的console.log(config.prop2);
...
}]);
This is a follow-up question to How to create this global constant to be shared among controllers in Angularjs?
The answer provided allows a constant $webroot to be shared among controllers.
app = angular.module('myApp', []);
app.constant('$webroot', 'localhost/webroot/app');
app.controller('myController', ['$scope', '$webroot', function($scope, $webroot) {
$scope.webroot = $webroot;
}]);
However, the problem is if I have 10 constants, then all 10 constants have to be injected into the controller. This makes the controller declaration look long and ugly. How can I create an object with properties that are sharable among controllers in AngularJS? In this way, I need only inject a single object instead of many constants. Can this be done in Angularjs? Thanks.
var app = angular.module('app', []);
app.constant('config', {
prop1: 'val1',
prop2: 'val2',
...
});
app.controller('Ctrl', ['config', function(config) {
console.log(config.prop1);
console.log(config.prop2);
...
}]);
这篇关于如何创建与在AngularJS控制器之间可共享特性的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!