1
在角度指令init函数中,我声明常量并在html中使用常量。我有多个指令利用相同的常量,我想保持常量是可重用的。 在下面的例子中,我在测试和test1指令中都使用scope.sName和scope.sType,我如何将它放在公共位置并重新使用它。在角度指令中优化代码
app.directive('test', function() {
return {
restrict:'A',
scope: { someVal: '='}
link: function(scope, element, attrs) {
scope.sType = InvertoryConstant.serviceType;
scope.sName = InvertoryConstant.serviceName;
}
}});
app.directive('test1', function() {
return {
restrict:'A',
scope: { someVal: '='}
link: function(scope, element, attrs) {
scope.sType = InvertoryConstant.serviceType;
scope.sName = InvertoryConstant.serviceName;
}
}});
把普通代码服务和将服务注入您的指令。 –
是“InvertoryConstant”的一个对象,因为它是一个常量,它的值在任何地方都不会改变? – Sravan
现在你有哪些常数? – Sravan