简体   繁体   English

给出每个角形材料复选框和ID

[英]Give every angular material checkbox and ID

I am currently having a problem: 我目前有一个问题:

I need to create md-checkboxes from a Database. 我需要从数据库创建md-checkboxes This part works fine with ng-repeat . 这部分可以与ng-repeat一起正常工作。 But I am having a problem reading those checkboxes. 但是我在阅读这些复选框时遇到了问题。 Every entry in the Database has its own unique ID (I am using RethinkDB) so I thought I just can apply this as an ID. 数据库中的每个条目都有其自己的唯一ID(我正在使用RethinkDB),所以我想我可以将其用作ID。

md-card(ng-repeat="n in ideas")
md-card-content
span
md-checkbox(type="checkbox" id='n.id') {{n.idea}}

I am working with Jade / Pug as View Engine. 我正在使用Jade / Pug作为View Engine。 But how am I now able to read out all checkboxes at once? 但是我现在如何能够一次读出所有复选框? I tried many methods like looping through all ElementsByTagName("md-checkbox") and than with a for to read the checked value but it always returns undefined. 我尝试了许多方法,例如遍历所有ElementsByTagName("md-checkbox")然后使用for读取已检查的值,但它始终返回未定义的值。

 const checkboxes = document.getElementsByTagName("md-checkbox");
 console.log(checkboxes) //works fine, prints all checkboxes
 for (let i = 0; i < checkboxes.length; i++) {
    console.log(checkboxes[i].checked); //returns undefined
 }

Do you have any Ideas how to read all Boxes at once? 您有如何一次阅读所有Box的想法吗?


Edit #1 - More Code 编辑#1-更多代码

index.js index.js

angular.module('votes', ['ngMaterial'])

.controller("VoteMainController", function ($scope) {
    $scope.safeApply = function (fn) {
        var phase = this.$root.$$phase;
        if (phase == '$apply' || phase == '$digest') {
            if (fn && (typeof(fn) === 'function')) {
                fn();
            }
        } else {
            this.$apply(fn);
        }
    };

    register = [];

    //var to store database content and add it to page
    $scope.ideas;

    //Downloading Database stuff as JSON
    $.ajax({
        type: "GET",
        url: "./api",
        async: true,
        success: function (content) {
            for (let i = 0; i < content.length; i++) {
                register.push({
                    [content[i].id]: {
                        checked: false
                    }
                })
            }
            $scope.ideas = content;
            $scope.safeApply();
        },
    });


    function checkChecked() {
        const checkboxes = document.getElementsByTagName("md-checkbox");
        for (let i = 0; i < checkboxes.length; i++) {
            console.log(checkboxes[i].checked);
        }
    }
})

index.jade index.jade

    form(id="login" method="post")
       md-card(ng-repeat="n in ideas")
          md-card-content
             span
             md-checkbox(type="checkbox" id='n.id') {{n.idea}}
          md-input-container
             label Andere Idee?
             md-icon search
             input(name='idea', ng-model='idea', id='idea', type='text')
         div(layout="column")
             md-button(type='submit', class="md-raised md-primary unselectable")
                 | Senden!

You should be able to do this with ng-list and ng-model : 您应该可以使用ng-listng-model做到这一点:

HTML HTML

<div ng-app="MyApp" ng-controller="MyAppController">
  <ng-list>
    <ng-list-item ng-repeat="n in ideas">
      <p>{{n.id}}: <input type="checkbox" ng-model="n.isChecked"></p>
    </ng-list-item>
  </ng-list>
  <p>Current state of model: {{ ideas }}</p>
</div>
<p>Current state of model: {{ ideas }}</p>

Angular

angular .module("MyApp", ['ngMessages']) .controller('MyAppController', AppController); angular .module(“ MyApp”,['ngMessages']).controller('MyAppController',AppController);

function AppController($scope) {
  $scope.ideas = [{
    id: 193872768,
    isChecked: false
  },{
    id: 238923113,
    isChecked: false
  }];
}

Demo 演示

This also works with the material design counterparts, md-list and md-checkbox . 这也适用于与材料设计相对应的md-listmd-checkbox

Your question title asks about assigning ids but Your question, which you wrote at the very end is 您的问题标题询问有关分配ID的问题,但您在最后写的问题是

"Do you have any Ideas how to read all Boxes at once?" “您有任何想法如何一次读取所有Box吗?”

So if you wanna do something with multiple or all checkbox elements you should assign them some css class, say "idea-checkbox". 因此,如果您想对多个或所有复选框元素进行操作,则应为它们分配一些CSS类,请说“ idea-checkbox”。

Then in css styles or in jQuery you can access all those checkboxes at once by using the dotted syntax of ".idea-checkbox". 然后,在CSS样式或jQuery中,您可以使用“ .idea-checkbox”的点分语法立即访问所有这些复选框。

css ids are used to distinctively access any one html element and classes are used to get access to all the elements at once which has that class. CSS ID用于区别地访问任何一个html元素,而类用于一次访问具有该类的所有元素。

You can also try to the use angular's "track by n.id" syntax if you want to track them by their id. 如果要按角度跟踪它们,请尝试使用angular的“ n.id跟踪”语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM