我是AngularJS世界中的新成员。我在这里有一个情况。在angularJS中获取html元素
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">
<table ng-controller="myCtrl" border="1">
<tr class="x" ng-repeat="x in records">
<td>{{x.Name}}</td>
<td>{{x.Country}}</td>
</tr>
</table>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = [
{
"Name" : "Alfreds Futterkiste",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"Country" : "Austria"
}
];
var arrays = document.getElementsByClassName("x");
console.log(arrays.length);
});
</script>
我试图(使用纳克 - 重复)来获取HTML元件阵列我没有通过angularJS负载。当我记录整个数组的值时,其结果是正确的。但是,当我登录array.length或array [1]时,结果分别为0和undefined。我试图把这些代码放在$ scope.on('viewContentLoaded')中,以确保所有html元素都完成加载,但结果是一样的。那么我的愚蠢在这里呢?
谢谢。
****解决方案:
$timeout(function(){
var arrays = document.getElementsByClassName("x");
console.log(arrays.length);
});
由于@Vladimir中号
为什么你不只是得到通过角数组:$ scope.records? $ scope.records.length? –
我只是想获取html元素来操纵它们; array.length不是我的目的;我只是用它来测试。 :) – Justin