gpt4 book ai didi

javascript - 如何在 Angular js中相互绑定(bind)两个不同数组的值

转载 作者:行者123 更新时间:2023-12-02 14:13:21 25 4
gpt4 key购买 nike

嗨,我将使用 Angular js 和 json 来管理用户访问级别。我有两个数组,一个用于菜单结构,另一个用于访问级别。

current access levels :

    $scope.current_access_levels ={
"can_home":{"title":"home","value":true},
"can_mail":{"title":"mail","value":false}
};

menu structure :

    $scope.menu = [
{"id":"1","name":"home","aclvl":"can_home"},
{"id":"2","name":"mail","aclvl":"can_mail"}
];

每个菜单字段都有一个“aclvl”,与“current_access_level”数组中的对象名称相匹配。

what i'm going to do is something like this :

<div ng-app="app" ng-controller="appCtrl">
<div ng-repeat="aclvl in current_access_levels">
<label>{{aclvl.title}}</label><input type="checkbox" value="1" ng-model="aclvl.value"/>
</div>
<br />
<br />
<ul>
<li ng-repeat="men in menu">
<input type="checkbox" ng-model="current_access_levels.(men.aclvl).value"/>{{men.aclvl}}
</li>
</ul>
</div>

例如,如果“men”的“aclvl”等于“can_home”,则复选框值应等于“aclvl.can_home.value”。任何人都知道我应该如何处理这个问题!?我添加了一个 fiddle 来明确我应该做什么。谢谢

angular.module('app',[])
.controller('appCtrl',function($scope){

$scope.current_access_levels ={"can_home":{"title":"home","value":true},"can_mail":{"title":"mail","value":false}};

$scope.menu = [{"id":"1","name":"home","aclvl":"can_home"},{"id":"2","name":"mail","aclvl":"can_mail"}];

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="app" ng-controller="appCtrl">
<!-- current access levels -->
<p>current access levels</p>
<div ng-repeat="aclvl in current_access_levels">
<label>{{aclvl.title}}</label><input type="checkbox" value="1" ng-model="aclvl.value"/>
</div>
<!-- current access levels -->
<br />
<div>{{current_access_levels}}</div>
<br />
<hr>
<!-- menu structure just for example -->
<p>menu structure</p>
<ul>
<li ng-repeat="men in menu">
<input type="checkbox" ng-model="current_access_levels.can_home.value"/>{{men.aclvl}}
</li>
</ul>
<!-- menu structure just for example -->

</div>

最佳答案

您可以将 []current_access_levels 一起使用,就像 -

 <li ng-repeat="men in menu">
<input type="checkbox" ng-model="current_access_levels[men.aclvl].value"/>{{men.aclvl}}
</li>

关于javascript - 如何在 Angular js中相互绑定(bind)两个不同数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com