Angular与ng-repeat斗争

时间:2016-05-11 18:01:39

标签: javascript angularjs angularjs-ng-repeat ng-repeat

当我只有一个项目时,我遇到了一些ng-repeat的问题。

这是我控制器中的一些部分代码:

Data.get('company').then(function(data){
        $scope.subDomains = data;
    });

这是代码运行之前的html:

<div id="subdomains">
    <ul>
        <li style="margin: 5px;" ng-repeat="sub in subDomains">{{sub.subdomain}} : <button type="button" class="btn btn-mav" ng-click="removeAccount(sub.subdomain);">Remove</button></li>
    </ul>
</div>

运行代码之后是我的html :(通知只有一个li有一个子域名)因此我只想显示一个li

<div id="subdomains">
<ul>
<!-- ngRepeat: sub in subDomains -->

<li style="margin: 5px;" ng-repeat="sub in subDomains" class="ng-binding ng-scope"> : <button type="button" class="btn btn-mav" ng-click="removeAccount(sub.subdomain);">Remove</button></li>
<!-- end ngRepeat: sub in subDomains -->

<li style="margin: 5px;" ng-repeat="sub in subDomains" class="ng-binding ng-scope">jason : <button type="button" class="btn btn-mav" ng-click="removeAccount(sub.subdomain);">Remove</button></li>
<!-- end ngRepeat: sub in subDomains -->

<li style="margin: 5px;" ng-repeat="sub in subDomains" class="ng-binding ng-scope"> : <button type="button" class="btn btn-mav" ng-click="removeAccount(sub.subdomain);">Remove</button></li>
<!-- end ngRepeat: sub in subDomains -->

<li style="margin: 5px;" ng-repeat="sub in subDomains" class="ng-binding ng-scope"> : <button type="button" class="btn btn-mav" ng-click="removeAccount(sub.subdomain);">Remove</button></li>
<!-- end ngRepeat: sub in subDomains -->

<li style="margin: 5px;" ng-repeat="sub in subDomains" class="ng-binding ng-scope"> : <button type="button" class="btn btn-mav" ng-click="removeAccount(sub.subdomain);">Remove</button></li>
<!-- end ngRepeat: sub in subDomains -->
</ul>
</div>

这是控制台日志:

Object {apiversion: "2", data: Object, event: Object, func: "listsubdomains", module: "SubDomain"}
    apiversion:"2"
    data:Object
      basedir:"public_html"
      dir:"/home/XXXXX/public_html"
      domain:"jason.XXXXX.com"
      domainkey:"jason_XXXXX.com"
      reldir:"home:public_html"
      rootdomain:"XXXXX.com"
      status:"not redirected"
      subdomain:"jason"

我试图仅列出子域名。如果我有一个以上,它可以完美地工作,但如果我只有一个,我会得到我发布的结果。

2 个答案:

答案 0 :(得分:2)

如果您使用object member子域对象,则每DateTimeDesc = "Date desc"个重复一次。确保子域始终是数组,即使只存在一个或没有项目。

答案 1 :(得分:1)

尝试以下

Data.get('company').then(function(data){
    if(angular.isArray(data))
        $scope.subDomains = data;
    else
        $scope.subDomains = [data];

});

希望这个帮助