如何引用值与来自不同模块的同名 [英] How to reference values with the same name from different modules
问题描述
如果我有一些模块,定义了相同的值
对象:
If I have some modules which defines the same value
object:
var m1 = angular.module('m1', []);
m1.value('test', 'AAA');
var m2 = angular.module('m2', []);
m2.value('test', 'BBB');
通知 M1
和 2
都具有相同的值测试
。
然后在主模块中,我依靠他们二:
Then in the main module, I depend on them two:
var app = angular.module('angularjs-starter', ['m1','m2']);
app.controller('MainCtrl', function($scope, test) {
$scope.test = test;
});
和HTML是很简单的:
And the HTML is very simple:
<body ng-controller="MainCtrl">
[{{test}}]
</body>
这将在最后一页显示 [BBB]
。我可以看到,值测试
M1
已推翻了由 2 <之一/ code>。
It will show[BBB]
in the final page. I can see that the value test
of m1
has been overrode by the one of m2
.
您可以看到现场演示在这里:<一href=\"http://plnkr.co/edit/u7u8p0nYqq9CvNxWKv5G?p=$p$pview\">http://plnkr.co/edit/u7u8p0nYqq9CvNxWKv5G?p=$p$pview
You can see a live demo here: http://plnkr.co/edit/u7u8p0nYqq9CvNxWKv5G?p=preview
有什么办法来显示测试值
无论从 M1
和 2在同一页
?
Is there any way to show the values of test
both from m1
and m2
in the same page?
推荐答案
在短期 - 否即可。 AngularJS模块形成一个命名空间。如果你用相同的名字2不同的模块定义2个值只有一个会在运行时可见。这适用于任何供应商,不但值。
In short - no. AngularJS modules form one namespace. If you define 2 values with the same name on 2 different modules only one will be visible during runtime. This applies to any providers, not only values.
这可能会在AngularJS的未来版本,但现在得到解决您最好的选择是preFIX你的价值观(和其他供应商)与模块名称
This might get addressed in future versions of AngularJS but for now your best option is to prefix your values (and other providers) with a module name.
这篇关于如何引用值与来自不同模块的同名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!