javascript - 使用 NOT(!) 运算符时出现“赋值左侧无效”错误

标签 javascript angularjs logical-operators

我正在使用这个功能:

$scope.myFunction = function(indexParent, childrenLength) {

    // close all inner accordion tabs
     for(i=0; i < childrenLength; i++) {
       !$scope.lettersandnumbers[indexParent].things[i].open = $scope.lettersandnumbers[indexParent].things[i].open;
     }

    // toggle parent tab
    $scope.lettersandnumbers[indexParent].open = !$scope.lettersandnumbers[indexParent].open;

 }

这里:<button ng-click="myFunction(0, 3)">Toggle a</button> 然而,“关闭所有内部 Accordion 选项卡”部分给了我分配错误中无效的左侧。如何更改代码以使其正常工作?

https://plnkr.co/edit/TlKhBZer1wYMW0XXBcqO?p=preview

非常感谢

更新

进行一些修改后回答:https://plnkr.co/edit/aMD5rGxpe48lziTb6xPk?p=preview

最佳答案

您的代码应如下所示

$scope.myFunction = function(indexParent, childrenLength) {

    // close all inner accordion tabs
     for(i=0; i < childrenLength; i++) {
       $scope.lettersandnumbers[indexParent].things[i].open = !$scope.lettersandnumbers[indexParent].things[i].open;
     }

    // toggle parent tab
    $scope.lettersandnumbers[indexParent].open = !$scope.lettersandnumbers[indexParent].open;

 }

! 位于分配的左侧

编辑:

证明你不能在左侧使用!:

var a = true;
console.log(a);
!a = a;

编辑2:

$scope.myFunction = function(indexParent, childrenLength) {

  // First close the outer tab
   $scope.lettersandnumbers[indexParent].open = !$scope.lettersandnumbers[indexParent].open;

  // Close all inner tabs if the outer tab is closed
  for(i=0; i < childrenLength; i++) {
    $scope.lettersandnumbers[indexParent].things[i].open = !$scope.lettersandnumbers[indexParent].open ? false : $scope.lettersandnumbers[indexParent].things[i].open;
  }
}

这里我使用 ternary operator以确定是否应关闭内部选项卡。

我在您的代码中看到 myFunction 仅在按钮上调用,您应该在单击外部 Accordion 时找到使其工作的方法

关于javascript - 使用 NOT(!) 运算符时出现“赋值左侧无效”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679682/

相关文章:

javascript - 当我有 2 个模态时,如何在 Bootstrap 中关闭模态(弹出屏幕)

javascript - Yii2 HTML 帮助器输入 make 需要

javascript - 使用 Angular 的解析 : {}? 时链接 promise 的最佳方式是什么

javascript - 服务调用后 Angular 范围未更新

javascript - 使用后备时控制台中的 Angular 抑制 404?

python - Pandas 数据框中的逻辑或/按位或

javascript - 在 javascript 中链接逻辑运算符的简写?

javascript - 确定限制的智能方法

javascript - MongoDB 触发器文件中的代码是 Producing a Customer.findOne() is not a function 错误

javascript - 在javascript中从倒计时中添加/减去时间