[英]How to assign value and scope to two concatenated string
我的HTML代碼:-
<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
和value分配給串聯的字符串?
代替這個(不正確的語法):
$scope.(list+"MenuHide") = true;
您需要這樣做:
$scope[list + "MenuHide"] = true;
要在對象內部動態更改屬性,您需要使用[]
包裝屬性名稱,然后直接在對象上調用它。 由於$scope
在這里是JavaScript對象,因此將在$scope
中將屬性list + "MenuHide"
分配為true
。
從MDN 計算出的屬性名稱 :
從ECMAScript 2015開始,對象初始化程序語法還支持計算的屬性名稱。 這樣就可以將表達式放在方括號[]中,該表達式將被計算並用作屬性名稱。
更改$scope.(list+"MenuHide") = true;
至
list = list + "MenuHide";
$scope[list] = true;
或:單行$scope[list+ "MenuHide"] = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.