【问题标题】:angularjs nested array undefinedangularjs嵌套数组未定义
【发布时间】: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


    【解决方案1】:

    您可以将$scope.users更改为以下代码:

      $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"}
                ]}
            ];
    

    【讨论】:

    • 你打败了我 30 秒我正在创建一个 plunker :-P
    • 正确的做法是在 cmets 中提出这个建议,并与有足够代表(如 @squiroid)的人一起建议由于简单的错字而作为“离题”结束
    【解决方案2】:

    您的类型在 $scope.users 之后缺少“=”

    $scope.users=[
                        {id: "1",
                            name: "userAbc",
                            location: "hugecity",
                            web: "www.abc.nl",
                            bio: "Hey welcome on my profile",
                            image: "Hello",
                            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: "kjh",
                    tweets: [
                        {date: "08/03/2015", text: "bericht 4"},
           [enter link description here][1]             {date: "07/03/2015", text: "bericht 3"},
                        {date: "06/03/2015", text: "bericht 2"},
                        {date: "05/03/2015", text: "bericht 1"}
                    ]}
                ];
            }]);
    

    Plunker

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 2019-04-19
      • 2021-10-23
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多