我目前有一個問題:給每個角材複選框和ID
我需要從數據庫創建md-checkboxes
。這部分工作正常,ng-repeat
。但我在閱讀這些複選框時遇到問題。 數據庫中的每個條目都有自己唯一的ID(我正在使用RethinkDB),所以我認爲我可以將其作爲ID應用。
md-card(ng-repeat="n in ideas")
md-card-content
span
md-checkbox(type="checkbox" id='n.id') {{n.idea}}
我正在用Jade/Pug作爲View Engine。 但是我現在怎麼能夠一次讀出所有的複選框呢? 我嘗試了很多方法,比如循環遍歷所有的ElementsByTagName("md-checkbox")
,並用for來讀取選中的值,但它總是返回undefined。
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
}
你有什麼想法如何一次讀取所有的盒子?
編輯#1 - 更多的代碼
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
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!
你可以添加你的完整代碼,HTML和所有?我也無法在任何地方看到你使用'scope'。 – ZombieChowder
for循環不是異步的,如果它的數據來自數據庫,它應該是angular.foreach並且請添加更多代碼 – alphapilgrim
@ ZombieChowder增加了更多代碼! – dunklesToast