如何将值和范围分配给两个串联字符串



My HTML Code:-

<ul class="sub-menu collapse">
<li ng-hide="abcMenuHide" ng-click="showModal('abcMaster')">
<a>Add/update listOne</a></li>
<li ng-hide="xyzMenuHide" ng-click="showModal('xyzMaster')">
<a>Add/Update listTwo</a></li>
</ul> ... and so on

我正在尝试根据分配给ng-hide的值隐藏<li>元素。 我在这样的指令中制作这个值:-

$scope.menuListHide = function(){
for (var group in $scope.data) {
if (group != "sectionOne") {
for (var list in $scope.data[group]) { 
for (var key in $scope.data[group][list]) { 
if ($scope.data[group][list].view != undefined) {
if (!$scope.data[group][list].view) {
$scope.(list+"MenuHide") = true; \This assignment is wrong also I am not able to attach scope to it.
}
}
else if($scope.data[group][list][key].view != undefined){
if (!$scope.data[group][list][key].view) {
$scope.(key+"MenuHide") = true; \This assignment is wrong also I am not able to attach scope to it.
}
}
}
}
}   
}
}
$scope.menuListHide();

有什么方法可以为串联字符串分配$scope和值?

而不是这个(语法不正确(:

$scope.(list+"MenuHide") = true;

您需要这样做:

$scope[list + "MenuHide"] = true;

若要在对象内动态更改属性,需要使用[]包装属性名称,并直接在对象上调用它。由于$scope在这里是一个 JavaScript 对象,因此属性list + "MenuHide"将在$scope中分配给true

来自 MDN 计算的属性名称:

从 ECMAScript 2015 开始,对象初始值设定项语法还支持计算属性名称。这允许您将表达式放在括号 [] 中,该表达式将被计算并用作属性名称。

更改$scope.(list+"MenuHide") = true;

list = list + "MenuHide";
$scope[list] = true;

或者:单行$scope[list+ "MenuHide"] = true;

最新更新