gpt4 book ai didi

javascript - Angular 与 ng-repeat 斗争

转载 作者:行者123 更新时间:2023-11-29 19:13:21 26 4
gpt4 key购买 nike

当我只有一项时,我在使用 ng-repeat 时遇到了一些问题。

这是我的 Controller 中的部分代码:

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"

我试图只列出子域。当我有多个时,它会完美运行,但如果我只有一个,我会得到我发布的结果。

最佳答案

如果您使用 ng-repat="sub in subDomains" 并且 subDomains 是一个对象,那么您将有一个重复对象成员。确保您的 subDomains 始终是一个数组,即使只有一个或没有项目存在。

关于javascript - Angular 与 ng-repeat 斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37170165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com