有条件的ng-单击AngularJS

时间:2018-10-17 10:03:13

标签: javascript angularjs

我在ng-repeat里面有一个代码

<tr ng-repeat="location in assetLocations track">     
    <span class="fa fa-remove fa-lg text-danger" style="cursor: pointer;"
                        ng-show="location.isEdit"
                        ng-click="readOnly===false ? (cancel();location.isEdit = false; updateClicked = false;) : null"></span>
</tr>

此处location.isEdit属性是动态设置的。

我想将有条件的ng-click设置为span。但是上面的代码给出了以下错误

  
    

angular.js:11598错误:[$ parse:syntax]语法错误:令牌';'是意外的,>> expressioning []]在表达式[readOnly ==== false的第29列? >>(cancel(); location.isEdit = false; updateClicked = false;):null]从>> [; location.isEdit = false; updateClicked = false;):null]。

  

我可以在函数内设置变量,但是我需要设置变量并在ng-click上调用函数

在这种情况下如何使用条件ng-click?

2 个答案:

答案 0 :(得分:2)

您可以尝试以下代码,

控制器

$scope.conditionCancelClick = function(location){
    if(!$scope.readOnly){
        $scope.cancel();
        location.isEdit = false;
        $scope.updateClicked = false;
    }
}

模板:

<tr ng-repeat="location in assetLocations track">  
    <span class="fa fa-remove fa-lg text-danger" style="cursor: pointer;" 
          ng-show="location.isEdit" ng-click="conditionCancelClick(location)"></span>
</tr>

答案 1 :(得分:0)

尝试替换;到,为

<tr ng-repeat="location in assetLocations track">     
    <span class="fa fa-remove fa-lg text-danger" style="cursor: pointer;"
                        ng-show="location.isEdit"
                        ng-click="readOnly===false ? (cancel(),location.isEdit 
= false, updateClicked = false) : null"></span>
</tr>