简体   繁体   English

angularjs嵌套数组未定义

[英]angularjs nested array undefined

I am trying to create a twitter clone with angularjs. 我正在尝试使用angularjs创建一个Twitter克隆。 For now the data is mocked in arrays. 目前,数据是在数组中模拟的。

How can I reach the nested array? 我如何到达嵌套数组? the only error I found was 我发现的唯一错误是

"Cannot read property '#' of undefined" “无法读取未定义的属性'#'”

The array is named users, which contains all of the data about a user, including the tweets in a nested array 该数组名为用户,其中包含有关用户的所有数据,包括嵌套数组中的推文

Or is there a way to reach a certain item using something like user[x].name where x is the array index 或者有没有办法使用诸如user [x] .name之类的东西到达某个项目,其中x是数组索引

The HTML looks like this: 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>

The data/array is stored like this: 数据/数组的存储方式如下:

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

You can change the $scope.users to the following code : 您可以将$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"}
            ]}
        ];

Your type missing "=" after $scope.users $ 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 柱塞

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

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