2017-01-05 59 views
-1

我有一個基於交換機的div,但交換機有一個布爾變量,但該值將根據row.id進行評估。有沒有人告訴我什麼,我做錯了什麼?Angular js:動態表達式不適用於ng-switch-

<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> 
+0

所以,有兩個問題後,你有什麼問題?能否請您提供反饋? – FRECIA

回答

0

您不需要===,將其刪除。

<div ng-switch="hasUrl"> 
    <a ng-switch-when="row.id.indexOf(':') < 0" href="{{url + row.id}}"> <!-- hasUrl = true --> 
     {{getName(row)}} 
    </a> 
    <a ng-switch-default href="......."> 
     {{getName(row)}} 
    </a> 

0

注意,要匹配的屬性值不能 表達式。它們被解釋爲文字字符串值,以匹配 反對。例如,NG-開關時=「someVal」將匹配靠在 字符串「someVal」不是針對表達 $ scope.someVal的值。

所以根據文檔採取下面的例子來解決你的情況:

<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> 

看carefullyat範圍變量及其值:

$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代替。

FIDDLE固定小提琴