Javascript 来自资源的 angular ng-repeat 复选框并设置为选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14474671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:54:29  来源:igfitidea点击:

angular ng-repeat checkbox from resource and set checked

javascriptangularjs

提问by daremachine

i have resource in my controller and populate checkboxes with ng-repeat next thing i know in controller which are to be checked

我的控制器中有资源,然后用 ng-repeat 填充复选框,接下来我在控制器中知道要检查的内容

code will looks like..

代码看起来像..

$scope.data = resource.query();

var arr_to_be_checked = [1,3,5];

in other controller i use this..

在其他控制器中我使用这个..

$scope.data = [ { Type: 1, checked: true, Title: 'Item A'},
                { Type: 2, checked: false, Title: 'Item B'}];

and its working fine but i need apply this on resource and ng-repeat because i need more flexible

它工作正常,但我需要将它应用于资源和 ng-repeat 因为我需要更灵活

i find any soulution but without point. Pls can you help me anyone?

我找到了任何解决方案,但毫无意义。请你能帮我任何人吗?

My question is: How i can "override" objects in resource with 'checked:true' or set any checkbox as checked.

我的问题是:如何使用“checked:true”“覆盖”资源中的对象或将任何复选框设置为选中状态。

Thank you so much for help or any idea
Have a nice day


非常感谢您的帮助或任何想法
祝您有美好的一天


回答by Umur Kontac?

<ul>
  <li ng-repeat="item in data">
    <input type="checkbox" ng-checked="item.checked"> {{item.Title}}
  </li>
</ul>
<button ng-click="checkItems()">Do</button> 

.

.

$scope.checkItems = function () {
  var i;
  for (i = 0; i < arr_to_be_checked.length; i++) {
    data[arr_to_be_checked[i]].checked = true;
  } 
};

PS: Please be really consistent on your naming conventions, javascript uses a camelCasenaming convention, no underscores. Only constructor functions are named PascalCase

PS:请在命名约定上保持一致,javascript 使用camelCase命名约定,没有下划线。仅命名构造函数PascalCase

Related API Documentation:
ng-click
ng-checked

相关API文档:
ng-click
ng-checked

回答by kolypto

Another solution:

另一种解决方案:

<div ng-controller="MainCtrl">
  <label ng-repeat="(color,enabled) in colors">
      <input type="checkbox" ng-model="colors[color]" /> {{color}} 
  </label>
  <p>colors: {{colors}}</p>

<script>
  var app = angular.module('plunker', []);

  app.controller('MainCtrl', function($scope){
      $scope.colors = {Blue: true, Orange: true};
  });

http://plnkr.co/edit/U4VD61?p=preview

http://plnkr.co/edit/U4VD61?p=preview

回答by daremachine

SOLUTION

i look at solution from fastreloadand modified like this..

解决方案

我从fastreload 中查看解决方案并像这样修改..

var arr_checked_items = [1,2,5] // this checkboxes will be set as checked


<ul>
  <li ng-repeat="item in data">
    <input type="checkbox" ng-checked="checkItem(item.id)"> {{item.Title}}
  </li>
</ul>

.

.

$scope.checkItem = function (id) {
   var checked = false;
   for(var i=0; i<= arr_to_be_checked.length; i++) {
      if(id == arr_to_be_checked[i]) {
         checked = true;
      }
    }
    return checked;
};

ITS WORKING PERFECT !!! on load without any clicking buttons

它的工作完美!加载时无需任何点击按钮