如果满足条件,则使用 ng-if 显示某些内容



如果满足条件,我正在尝试使用ng-if来显示某些行,但似乎无法让它 100% 正常工作。

<div style="margin-bottom: 1em;">
  <h4><!-- content omitted for brevity--></h4>
  <p>
    <strong>Covered:</strong> &nbsp;
    <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&nbsp;<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因为可能是nullundefined(这取决于您的控制器逻辑(......但是,为了解决这个问题,请尝试:

ng-if="life.beneficiary.length = 0"更改为ng-if="!life.beneficiary.length"(这相当于ng-if="life.beneficiary.length == 0",但如果life.beneficiary nullundefined,则逻辑不会失败(

<a ng-href class="pointer" ng-if="!life.beneficiary.length" ng-click="c.addBeneficiary()">Add Beneficiary&nbsp;
</a>

此外,建议将两个链接(<a>(包装在一个元素(spandiv,...如你所愿(,这将包含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&nbsp;</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"> &nbsp;<strong>Insured?</strong> &nbsp;&nbsp;<font size=2>
        <i class="fa {{data.icon}}" style="color:{{data.color}}" /> {{data.status}}
    </font>&nbsp;&nbsp;
</h4>
<p>
    <strong>Covered:</strong> &nbsp;
    <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">&nbsp;and&nbsp;</font>{{life.beneficiary}}&nbsp;&nbsp;
        <i class="fa fa-edit" /><font ng-show="!$last">,&nbsp;</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&nbsp;
        <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"> &nbsp;<strong>Insured?</strong> &nbsp;&nbsp;<font size=2>
        <i class="fa {{data.icon}}" style="color:{{data.color}}" /> {{data.status}}
    </font>&nbsp;&nbsp;
</h4>
<p>
    <strong>Covered:</strong> &nbsp;
    <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">&nbsp;and&nbsp;</font>{{life.beneficiary}}&nbsp;&nbsp;
            <i class="fa fa-edit" /><font ng-show="!$last">,&nbsp;</font>
        </a>
        <a ng-href class="pointer" ng-if="life.beneficiary.length = 0" ng-click="c.addBeneficiary()">
            Add Beneficiary&nbsp;
            <i class="fa fa-plus-circle" style="color: green;" />
        </a>
    </div>
</p>

**

    使用 life.beneficiary.length
  1. === 0 代替 life.beneficiary.length = 0
  2. = 用于赋值,== 或 === 用于比较。

或者,您也可以使用此优化逻辑。

  1. ng-if="life.beneficiary.length">

如果受益人长度为 0,则链接将被隐藏,否则将显示。

**

<a ng-href class="pointer" ng-if="life.beneficiary.length === 0" ng-click="c.addBeneficiary()">Add Beneficiary&nbsp;<i class="fa fa-plus-circle" style="color: green;"/></a></p>

相关内容

  • 没有找到相关文章

最新更新