AngularJS-ng开关中的多个条件
我有一个相当长的ng switch when语句,有些语句重复匹配 我读过angular不支持多值ng开关(im使用v1.2.8),但发现了这篇文章:分享如何修改库 我已经浏览了我的angular库,找到了ngSwitchWhenDirective:AngularJS-ng开关中的多个条件,angularjs,angularjs-directive,angularjs-scope,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Scope,Angularjs Ng Repeat,我有一个相当长的ng switch when语句,有些语句重复匹配 我读过angular不支持多值ng开关(im使用v1.2.8),但发现了这篇文章:分享如何修改库 我已经浏览了我的angular库,找到了ngSwitchWhenDirective: var ngSwitchWhenDirective = ngDirective({ transclude: 'element', priority: 800, require: '^ngSwitch', link:
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;
});
});
config(函数($routeProvider,$provide){
/**
*在以下情况下覆盖angular的指令:
*当=“value1 | | value2 | | value3”时可以处理ng开关
*/
$provide.decorator('ngSwitchWhenDirective',function($delegate){
$delegate[0]。compile=函数(元素、属性、转包){
返回函数(范围、元素、属性、ctrl){
var subCases=[attrs.ngSwitchWhen];
if(attrs.ngSwitchWhen&&attrs.ngSwitchWhen.length>0&&attrs.ngSwitchWhen.indexOf(“| |”)!=-1){
subCases=attrs.ngSwitchWhen.split(“| |”);
}
var i=0;
瓦卡塞;
var len=子类别长度;
while(iafaik)当
多个时,您可以在ng开关中使用相同的条件times@Alp-我不知道这一点,你有一个例子链接吗?我相信我以前在我的一个项目中做过。只是尝试一下。可能是重复的