javascript - Angular JS isNumber on ng-switch-当不工作时

标签 javascript html angularjs

我是 Angular 的新手。我正在尝试根据某些条件切换 div :

  <td>
    <div layout="row" layout-align="start center">
      <img ng-if="row.pin" ng-src="{{pinImageUrl}}" alt="Pinned @ {{row.pin}}" class="img-pin" />
      <div ng-switch="checkUrl">
        <a ng-switch-when="{{isNumber(row.id)}}" href="numberUrl ">
          {{getName(row)}}
        </a>
        <a ng-switch-when="url" href="{{url + row.id}}">
          {{getName(row)}}
        </a>
        <span ng-switch-default href="aaaaaa">
          {{getName(row)}}
        </span>
      </div>
    </div>
  </td>

但是 {{isNumber(row.id)}} 不工作。谁能告诉我这里出了什么问题?

最佳答案

  <div ng-switch="checkUrl">
    <a ng-switch-when="{{isNumber(row.id)}}" href="numberUrl ">
      {{getName(row)}}
    </a>
    <a ng-switch-when="url" href="{{url + row.id}}">
      {{getName(row)}}
    </a>
    <span ng-switch-default href="aaaaaa">
      {{getName(row)}}
    </span>
  </div>

ng-switch 中的expression 可以是作用域函数调用、作用域变量,也可以是一个值。但是在 ng-switch-when="something" 中,这里的 something 应该是一个值。即使我们提到范围变量或函数调用,它也不起作用。你提到的,它将它作为一个值,然后比较 ng-switch 中的值,如果匹配则呈现。但是在您的代码中,您在 ng-switch-when 中调用函数 isNumber()。所以,它不匹配。即使在第二种情况下,它也应该是 url 的值,而不是变量。但是,如果您想使用函数来呈现 DOM,请使用 ng-if 而不是 ng-switch。 ng-if="funcA()" 会起作用,但会在每个摘要周期执行(监视)以了解值。但是在 ng-if 中调用函数也不是一个好的解决方案。更多信息 ng-if being called more times than it should

关于javascript - Angular JS isNumber on ng-switch-当不工作时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309456/

相关文章:

javascript - 如何在不使用 iframe 或库的情况下创建裁剪显示窗口区域?

提供 HTML 或 Javascript 反编译器

html - Angularjs 表 nth-of-type

javascript - AngularJS ngRoute subview 未在父 View 中加载 Javascript

javascript - 发布到墙 facebook api 在本地工作但不是实时的

javascript - 谷歌地图js api在浏览器上工作但在cordova应用程序上不起作用

javascript - 为什么 someInputElement.type = 'button' ; IE 失败?

javascript - 使用 Javascript 将内容附加到 div 不起作用

html - 如何让一个元素跨越整个div?

angularjs - 基本参数为 `params`参数。 UI-路由器