本文介绍了Angular js:动态表达式不适用于ng-switch-when的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个基于switch的div,但是switch有一个布尔变量,但该值将基于row.id进行评估.有人可以告诉我我在做什么错吗?
I have a div based on switch but the switch has a boolean variable but the value will be evaluated based on the row.id. Can some one tell me what I am doing wrong here ?
<div ng-switch="hasUrl">
<a ng-switch-when="row.id.indexOf(':') < 0 === true" href="{{url + row.id}}"> <!-- hasUrl = true -->
{{getName(row)}}
</a>
<a ng-switch-default href=".......">
{{getName(row)}}
</a>
</div>
推荐答案
因此,根据文档采取以下示例来解决您的情况:
So according to the docs take following example to solve your case:
<div ng-switch="hasUrl">
<span ng-switch-when="row.id.indexOf(':') < 0"> WONT SHOW </span> <!-- WILL NOT WORK EVER -->
<span ng-switch-when="makeItWork"> ALSO, WONT SHOW</span>
<span ng-switch-when="true">WILL NOT SHOW EITHER</span>
<span ng-switch-when="1">WILL SHOW</span>
</div>
仔细查看范围变量及其值:
Look carefullyat scope variables and their values:
$scope.hasUrl = 1; /* NOTICE != true BUT 1*/
$scope.row = {};
$scope.row.id = "true:";
$scope.makeItWork = $scope.row.id.indexOf(':') > 0 ? 1 : 0;
console.log($scope.makeItWork); /* SEE THAT TRUE WILL BE LOGGED BUT IT STILL WONT SHOW */
因此,即使ng-switch
将对表达式求值,但ng-switch-when
似乎也不会.如果我是你,我只会坚持使用ng-if
.
So even though ng-switch
will evaluate an expression, it seems ng-switch-when
wont. If I were you I would just stick to ng-if
instead.
FIDDLE 固定小提琴
这篇关于Angular js:动态表达式不适用于ng-switch-when的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!