我正在使用这个功能:
$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/