如何在角度js中将两个不同数组的值彼此绑定

时间:2022-06-01 20:03:36

hi i'm going to manage the user access levels with angular js and json. i have two arrays one for menu structure and the other for access levels.

嗨,我将用角度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"}
    ];

every menu field has an "aclvl" that matches with the names of objects in "current_access_level" array.

每个菜单字段都有一个“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>

for example if the "aclvl" of "men" is equal to "can_home" the checkbox value should be equal to "aclvl.can_home.value". any body knows how should i handle this!? i have added a fiddle to clear what i'm supposing to do. thanks

例如,如果“men”的“aclvl”等于“can_home”,则复选框值应等于“aclvl.can_home.value”。任何人都知道我该怎么处理这个!?我添加了一个小提琴来清除我想要做的事情。谢谢

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>

1 个解决方案

#1


2  

You can use [] with current_access_levels it like as -

您可以将[]与current_access_levels一起使用,如下所示 -

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

#1


2  

You can use [] with current_access_levels it like as -

您可以将[]与current_access_levels一起使用,如下所示 -

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