如果满足条件,我正在尝试使用ng-if
来显示某些行,但似乎无法让它 100% 正常工作。
<div style="margin-bottom: 1em;">
<h4><!-- content omitted for brevity--></h4>
<p>
<strong>Covered:</strong>
<a ng-href class="pointer" data-ng-repeat="life in data.list2 track by $index" ng-if="life.beneficiary.length > 0" ng-click="c.editBeneficiary(life)">
<!-- content omitted for brevity-->
</a>
<a ng-href class="pointer" ng-if="life.beneficiary.length = 0" ng-click="c.addBeneficiary()">Add Beneficiary <i class="fa fa-plus-circle" style="color: green;"/></a></p>
</div>
在上面的代码中,如果受益人数大于 0 ( ng-if="life.beneficiary.length > 0"
(,我想列出名称并允许用户在必要时编辑它们。这部分工作正常。 但是,如果受益人数等于 0,那么我希望有一个显示"添加受益人"的链接,并允许用户这样做。
我将ng-if
语句放在<a>
标签中,我不确定这是否正确。 当我使用没有受益人的用户登录时,"添加受益人"链接不会显示。
- 我在这里做错了什么?
问题是life.beneficiary.length = 0
不是比较,而是试图将0
(零(值分配给life.beneficiary.length
(这是错误的(。
此外,当没有受益人时life.beneficiary.length == 0
(这是正确的(可能是伪造的life.beneficiary
因为可能是null
或undefined
(这取决于您的控制器逻辑(......但是,为了解决这个问题,请尝试:
将ng-if="life.beneficiary.length = 0"
更改为ng-if="!life.beneficiary.length"
(这相当于ng-if="life.beneficiary.length == 0"
,但如果life.beneficiary
null
或undefined
,则逻辑不会失败(
<a ng-href class="pointer" ng-if="!life.beneficiary.length" ng-click="c.addBeneficiary()">Add Beneficiary
</a>
此外,建议将两个链接(<a>
(包装在一个元素(span
,div
,...如你所愿(,这将包含ng-repeat
,像这样:
<p>
<!-- there is content omitted for brevity -->
<span data-ng-repeat="life in data.list2 track by $index">
<a class="pointer" ng-if="life.beneficiary.length > 0" ng-click="c.editBeneficiary(life)">
<!-- content omitted for brevity-->
</a>
<a class="pointer" ng-if="!life.beneficiary.length" ng-click="c.addBeneficiary()">Add Beneficiary </a>
</span>
</p>
尝试更改此设置
ng-if="life.beneficiary.length = 0"
在
ng-if="life.beneficiary.length == 0"
按照您当前的设置方式,它只会显示满足第一个ng-if
要求的<a>
标签。您的两个 <a>
标签都需要一个 ng-repeat
语句以及正确的 ng-if
语句:
<div style="margin-bottom: 1em;">
<h4>
<img class="icon" src="basic_life.png" height="30" width="30"> <strong>Insured?</strong> <font size=2>
<i class="fa {{data.icon}}" style="color:{{data.color}}" /> {{data.status}}
</font>
</h4>
<p>
<strong>Covered:</strong>
<a ng-href class="pointer" data-ng-repeat="life in data.list2 track by $index" ng-if="life.beneficiary.length > 0"
ng-click="c.editBeneficiary(life)">
<font ng-show="$last && !$first"> and </font>{{life.beneficiary}}
<i class="fa fa-edit" /><font ng-show="!$last">, </font>
</a>
<a ng-href class="pointer" data-ng-repeat="life in data.list2 track by $index" ng-if="!life.beneficiary.length" ng-click="c.addBeneficiary()">
Add Beneficiary
<i class="fa fa-plus-circle" style="color: green;" />
</a>
</p>
如果你想让它在单个ng重复下,你需要做这样的事情:
<div style="margin-bottom: 1em;">
<h4>
<img class="icon" src="basic_life.png" height="30" width="30"> <strong>Insured?</strong> <font size=2>
<i class="fa {{data.icon}}" style="color:{{data.color}}" /> {{data.status}}
</font>
</h4>
<p>
<strong>Covered:</strong>
<div data-ng-repeat="life in data.list2 track by $index">
<a ng-href class="pointer" ng-if="life.beneficiary.length > 0"
ng-click="c.editBeneficiary(life)">
<font ng-show="$last && !$first"> and </font>{{life.beneficiary}}
<i class="fa fa-edit" /><font ng-show="!$last">, </font>
</a>
<a ng-href class="pointer" ng-if="life.beneficiary.length = 0" ng-click="c.addBeneficiary()">
Add Beneficiary
<i class="fa fa-plus-circle" style="color: green;" />
</a>
</div>
</p>
**
- 使用 life.beneficiary.length
- === 0 代替 life.beneficiary.length = 0
- = 用于赋值,== 或 === 用于比较。
或者,您也可以使用此优化逻辑。
- ng-if="life.beneficiary.length">
如果受益人长度为 0,则链接将被隐藏,否则将显示。
**
<a ng-href class="pointer" ng-if="life.beneficiary.length === 0" ng-click="c.addBeneficiary()">Add Beneficiary <i class="fa fa-plus-circle" style="color: green;"/></a></p>