在下面的代码中,我从以前存储的localStorage中获取环境,并将其分配给$ scope.environments。当accessMethod为“ A”时,它应该显示按钮,而当accessMethod为“ B”时,它应该显示一个下拉菜单。
<form accept-charset="UTF-8" role="form" name="loginForm" ng-controller="PostsCtrl" novalidate ng-submit="Onsubmit()" >
<fieldset>
<ul class="heroes" ng-repeat="environment in environments track by $index" >
<li ng-if="environment.accessMethod === 'A'">
<button type="button" class="btn btn-primary" uib-dropdown-toggle ng-disabled="disabled">
<span class="badge">{{environment.code}}</span>
</button>
</li>
<li ng-if="environment.accessMethod === 'B'">
<div class="btn-group" uib-dropdown >
<button type="button" class="btn btn-primary" uib-dropdown-toggle ng-disabled="disabled">
<span class="badge">{{environment.code}}</span> <span class="caret"></span><span class="sr-only"></span>
</button>
<ul class="dropdown-menu" uib-dropdown-menu role="menu" >
<li ng-repeat="roledetail in environment.roles"
ng-click="onSelect(roledetail.roleName)" >
<a href="" >{{roledetail.roleName}}</a>
</li>
</ul>
</div>
</li>
</ul>
</fieldset>
</form>
$ scope.environments = localStorage.getItem(“ environments”);
我保留了Json的信息,但我正在通过localStorage访问它。
$scope.environments = [{
"code": "abc",
"name": null,
"accessMethod": "A",
"roles": [{
"roleId": 1,
"roleName": "A"
},
{
"roleId": 3,
"roleName": "B"
}
]
}, {
"code": "XYZ",
"name": null,
"accessMethod": "A",
"roles": [{
"roleId": 1,
"roleName": "A"
},
{
"roleId": 3,
"roleName": "B"
}
]
}, {
"code": "Neo",
"name": null,
"accessMethod": "B",
"roles": [{
"roleId": 1,
"roleName": "A"
},
{
"roleId": 3,
"roleName": "C"
}
]
}, {
"code": "LAB",
"name": null,
"accessMethod": "B",
"roles": [{
"roleId": 1,
"roleName": "A"
},
{
"roleId": 3,
"roleName": "B"
}
]
}]
最佳答案
分配给您的范围时,您没有执行Json.parse。
做这个
$scope.environments = JSON.parse(localStorage.getItem("environments"));
关于javascript - 如何用ng-if比较字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48514592/