Pav*_*vel 32 angularjs ng-switch
是否可以在ng-switch-when中进行OR?
<div ng-repeat="w in windows" ng-show="visibleWindowId == w.id" ng-switch="w.type">
<div ng-switch-when="val1 **OR** val2">
sup
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果没有,怎么能完成上述?
谢谢 :)
sea*_*lea 14
现在可以使用ng-switch-when-separator1.5.10中的Angular文档中添加的内容:
<div ng-repeat="w in windows" ng-show="visibleWindowId == w.id" ng-switch="w.type">
<div ng-switch-when="val1|val2" ng-switch-when-separator="|">
sup
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这也会奏效
<div ng-repeat="w in windows" ng-switch="w.type == 'val1' || w.type == 'val2'">
<div ng-switch-when="true">
sup
</div>
</div>Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的指令代替ngSwitchWhen,允许您为单个标签指定多个案例.它还允许您指定动态值而不是纯静态值.
有一点需要注意,它只在编译时计算一次表达式,因此必须立即返回正确的值.为了我的目的,这很好,因为我想使用应用程序中其他地方定义的常量.它可能被修改为动态地重新评估表达式,但这需要更多的测试ngSwitch.
我使用角度1.3.15,但我用角度1.4.7进行了快速测试,它在那里工作得很好.
module.directive('jjSwitchWhen', function() {
// Exact same definition as ngSwitchWhen except for the link fn
return {
// Same as ngSwitchWhen
priority: 1200,
transclude: 'element',
require: '^ngSwitch',
link: function(scope, element, attrs, ctrl, $transclude) {
var caseStms = scope.$eval(attrs.jjSwitchWhen);
caseStms = angular.isArray(caseStms) ? caseStms : [caseStms];
angular.forEach(caseStms, function(caseStm) {
caseStm = '!' + caseStm;
ctrl.cases[caseStm] = ctrl.cases[caseStm] || [];
ctrl.cases[caseStm].push({ transclude: $transclude, element: element });
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
$scope.types = {
audio: '.mp3',
video: ['.mp4', '.gif'],
image: ['.jpg', '.png', '.gif'] // Can have multiple matching cases (.gif)
};
Run Code Online (Sandbox Code Playgroud)
模板
<div ng-switch="mediaType">
<div jj-switch-when="types.audio">Audio</div>
<div jj-switch-when="types.video">Video</div>
<div jj-switch-when="types.image">Image</div>
<!-- Even works with ngSwitchWhen -->
<div ng-switch-when=".docx">Document</div>
<div ng-switch-default>Invalid Type</div>
<div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30832 次 |
| 最近记录: |