Angular js:动态表达式不适用于ng-switch-when [英] Angular js : Dynamic expression not working for ng-switch-when

查看:235
本文介绍了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>

推荐答案

请注意,要匹配的属性值不能是 表达式.它们被解释为文字字符串值以进行匹配 反对.例如,ng-switch-when ="someVal"将与 字符串"someVal"不反对表达式的值 $ scope.someVal.

Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal.

因此,根据文档采取以下示例来解决您的情况:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆