我有一个相当长的ng-switch-when语句,还有一些重复匹配。
我已经读过,角度不支持多值ng-switch(即时使用v1.2.8 ),但已找到这篇文章:How can I use ng-switch to satisfy multiple, same conditions?分享如何图书馆可以修改。
我已经通过我的角度库找到 ngSwitchWhenDirective :
var ngSwitchWhenDirective = ngDirective({
transclude: 'element',
priority: 800,
require: '^ngSwitch',
link: function(scope, element, attrs, ctrl, $transclude) {
ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
ctrl.cases['!' + attrs.ngSwitchWhen].push({ transclude: $transclude, element: element });
}
});
但是根据angabriel的建议,不能看出如何调整它:
config(function($routeProvider, $provide) {
/**
* overwrite angular's directive ngSwitchWhen
* can handle ng-switch-when="value1 || value2 || value3"
*/
$provide.decorator('ngSwitchWhenDirective', function($delegate) {
$delegate[0].compile = function(element, attrs, transclude) {
return function(scope, element, attr, ctrl) {
var subCases = [attrs.ngSwitchWhen];
if(attrs.ngSwitchWhen && attrs.ngSwitchWhen.length > 0 && attrs.ngSwitchWhen.indexOf('||') != -1) {
subCases = attrs.ngSwitchWhen.split('||');
}
var i=0;
var casee;
var len = subCases.length;
while(i<len) {
casee = $.trim(subCases[i++]);
ctrl.cases['!' + casee] = (ctrl.cases['!' + casee] || []);
ctrl.cases['!' + casee].push({ transclude: transclude, element: element });
}
}
}
return $delegate;
});
});