AngularJS ng-repeat第一项始终为null

时间:2014-08-20 01:26:38

标签: json angularjs angularjs-ng-repeat

如主题中所提到的,当我在一个东西上使用ng-repeat时,第一个元素总是为null时会出现什么问题?我已经尝试了几种方法来摆脱这个问题,但似乎没有用。

我的所有ng-repeat数据都存储在JSON文件中,JSON文件将通过$ http调用传递到范围,如下所示:

        $http.get('index.php?r=exploreComment/WS_getExploreComments&topic_id=' + topic_id).success(function(data){
        $scope.comments = data;
        console.log('Comment Loaded');

正确加载所有内容,只是ng-repeat循环中的第一项始终为null ..

GET http://localhost/viralenz/resource/_explore/%7B%7Bphoto.image%7D%7D 404 (Not Found) 
GET http://localhost/viralenz/resource/_explore/%7B%7Bimage.image%7D%7D 404 (Not Found) 
GET http://localhost/viralenz/resource/_users/%7B%7Bcomment.user_id%7D%7D/avatar/%7B%7Bcomment.avatar%7D%7D 404 (Not Found) 

我认为这只是JSON数据的一个错误,但后来我尝试了图像,同样的问题也出现了......我无法添加图像所以我附上了一个链接......

http://i.stack.imgur.com/lsFDX.png

我做错了什么?

/////编辑了更多详情////

这是ng-repeat的样子

<div class="exploreCommentBlock" ng-repeat="comment in comments">
    <table style="width: 100%">
        <tr style="width: 100%">

            <td style="width: 5%" rowspan="2" valign="top"><img
                src="<?php echo UrlMgmt::getUserResourceUrl()?>{{comment.user_id}}/avatar/{{comment.avatar}}"
                , style='height: 64px; width: 64px; border-radius: 5px;' /></td>



            <td style="width: 70%"><span class="author_name">{{comment.username}}</span>

--AND--SO--ON--

这是JSON数据:

[{"comment_id":"92","user_id":"1","topic_id":"1","data":"lalalalalalaa","image_append":null,"rating":"1","datetime":"2014-08-19 13:03:58","username":"Kenny Yap","avatar":"14079353851"},
{"comment_id":"91","user_id":"1","topic_id":"1","data":"test blank post","image_append":null,"rating":"1","datetime":"2014-08-19 10:48:41","username":"Kenny Yap","avatar":"14079353851"},
{"comment_id":"28","user_id":"2","topic_id":"1","data":"test owner comment","image_append":null,"rating":"5","datetime":"2014-08-18 09:39:59","username":"Dummy User","avatar":"default_avatar.png"}]

2 个答案:

答案 0 :(得分:6)

如果您在ng-repeat(或一般页面)中使用img src="{{stuff}}",则在首次加载页面时会出现错误,{{stuff}}会出现未定义的错误。诀窍是使用img ng-src="{{stuff}}"代替,当变量未被定义时,它会正确处理。

答案 1 :(得分:0)

对您的代码进行进一步调查表明此处还有其他内容,可能不是我上面链接的ng-src内容。

具体来说,您正在ng-repeat内部进行图像处理。我在那里看看问题所在,如果你已经加载了用户的个人资料信息,你可能只是通过角度加载图像数据而不是通过php注入它