【发布时间】:2015-03-08 12:49:53
【问题描述】:
我正在尝试使用 angularjs 创建一个 Twitter 克隆。 目前,数据在数组中被模拟。
如何访问嵌套数组? 我发现的唯一错误是
“无法读取未定义的属性‘#’”
该数组名为 users,其中包含有关用户的所有数据,包括嵌套数组中的推文
或者有没有办法使用诸如 user[x].name 之类的东西到达某个项目,其中 x 是数组索引
HTML 如下所示:
<div ng-repeat="user in users">
<div class="row" >
<div class="col-md-9">
<fieldset>
<legend>S0</legend>
<img src="{{user.image}}" alt="no image" style="float:left;width:50px;height:50px;margin-right:20px;">
<h1>{{user.name}}</h1>
</fieldset>
</div>
<div class="col-md-3">
<fieldset>
<legend>S2 Details</legend>
Name: {{user.name}}<br>
Location: {{user.location}}<br>
Web: {{user.web}}<br>
Bio: {{user.bio}}
</fieldset>
</div>
<div class="col-md-9">
<fieldset>
<legend>S1 Tweets</legend>
<ul>
<li ng-repeat="tweet in user.tweets">
{{user.name}}
{{user.tweet.date}}
<br>
{{users[1].tweet.text}}
</li>
</ul>
</fieldset>
</div>
<div class="col-md-3">
<fieldset>
<legend>S3</legend>
</fieldset>
</div>
<div class="col-md-3">
<fieldset>
<legend>S4 Following</legend>
</fieldset>
</div>
</div>
数据/数组是这样存储的:
angular.module('myApp.tweets', ['ngRoute'])
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/tweets', {
templateUrl: 'tweets/tweets.html',
controller: 'tweetsCtrl'
});
}])
.controller('tweetsCtrl', ['$scope', function ($scope) {
$scope.users[
{id: "1",
name: "userAbc",
location: "hugecity",
web: "www.abc.nl",
bio: "Hey welcome on my profile",
image: "http://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg",
tweets: [
{date: "08/03/2015", text: "bericht 4"},
{date: "07/03/2015", text: "bericht 3"},
{date: "06/03/2015", text: "bericht 2"},
{date: "05/03/2015", text: "bericht 1"}
]},
{id: "2",
name: "userDef",
location: "hugecity",
web: "www.abc2.nl",
bio: "Hey welcome to my profile",
image: "http://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg",
tweets: [
{date: "08/03/2015", text: "bericht 4"},
{date: "07/03/2015", text: "bericht 3"},
{date: "06/03/2015", text: "bericht 2"},
{date: "05/03/2015", text: "bericht 1"}
]}
];
}]);
【问题讨论】:
标签: javascript html arrays angularjs nested