首页 > 解决方案 > 有条件地点击 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] 语法错误:令牌';' 出乎意料,>>expecting [)] 在表达式 [readOnly===false 的第 29 列?>>(cancel();location.isEdit = false; updateClicked = false;) : null] 从 >>[;location.isEdit = false; updateClicked = false;) : null]。

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

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

标签: javascriptangularjs

解决方案


你可以试试下面的代码,

控制器

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

推荐阅读