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