我的ng-if还没有工作

时间:2015-06-08 16:28:02

标签: angularjs angular-ng-if

我在获取ng-if工作时遇到了问题。它适用于同一文件中的其他位置,但不在免责声明行

<div id="wallet" class="row">
    <div class="details">
            <div class="row" ng-repeat="account in accounts">
                <h3 ng-if="account.cardName == 'Cash'">Cash</h3>
                <div class="metrics">
                    <div class="balance">
                        <h5>Current Balance</h5>
                        <a ng-if="!isImpersonator()" href="{{account.walletUrl}}" target="_blank">
                            <u ng-if="account.cardName == 'Cash'">
                                <span ng-if="account.ABS_BALANCE">{{account.ABS_BALANCE | currency}}</span>
                                <span ng-if="!account.ABS_BALANCE">$0.00</span>
                            </u>
                            <u ng-if="account.cardName == 'Award Points'">
                                <span ng-if="account.ABS_BALANCE">{{account.ABS_BALANCE | number}}</span>
                                <span ng-if="!account.ABS_BALANCE">0</span>
                            </u>
                        </a>
                        <a ng-if="isImpersonator()">
                            <u ng-if="account.cardName == 'Cash'">
                                <span ng-if="account.ABS_BALANCE">{{account.ABS_BALANCE | currency}}</span>
                                <span ng-if="!account.ABS_BALANCE">$0.00</span>
                            </u>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <p class="disclaimer" ng-if="account.cardName == 'Cash'">Cash may be deposited to your card up to 48 hours.</p>
</div>

如果account.cardName等于&#39; Cash&#39;,我只希望显示免责声明行。为什么ng-if工作在上面但不在免责声明线上?

提前致谢...

3 个答案:

答案 0 :(得分:0)

这是因为免责声明行超出了范围。该帐户在此时间不存在。您必须在ng-repeat块中移动免责声明行,其中存在帐户才能使其正常工作。

答案 1 :(得分:0)

    <p class="disclaimer" ng-if="account.cardName == 'Cash'">Cash may be deposited to your card up to 48 hours.</p>

在ng-repeat之外的div之外,它将无法访问。试试这种方式;

<div id="wallet" class="row">
    <div class="details">
            <div class="row" ng-repeat="account in accounts">
                <h3 ng-if="account.cardName == 'Cash'">Cash</h3>
                <div class="metrics">
                    <div class="balance">
                        <h5>Current Balance</h5>
                        <a ng-if="!isImpersonator()" href="{{account.walletUrl}}" target="_blank">
                            <u ng-if="account.cardName == 'Cash'">
                                <span ng-if="account.ABS_BALANCE">{{account.ABS_BALANCE | currency}}</span>
                                <span ng-if="!account.ABS_BALANCE">$0.00</span>
                            </u>
                            <u ng-if="account.cardName == 'Award Points'">
                                <span ng-if="account.ABS_BALANCE">{{account.ABS_BALANCE | number}}</span>
                                <span ng-if="!account.ABS_BALANCE">0</span>
                            </u>
                        </a>
                        <a ng-if="isImpersonator()">
                            <u ng-if="account.cardName == 'Cash'">
                                <span ng-if="account.ABS_BALANCE">{{account.ABS_BALANCE | currency}}</span>
                                <span ng-if="!account.ABS_BALANCE">$0.00</span>
                            </u>
                        </a>
                    </div>
                </div>
            </div>
              <p class="disclaimer" ng-if="account.cardName == 'Cash'">Cash may be deposited to your card up to 48 hours.</p>
        </div>
    </div>
</div>

答案 2 :(得分:0)

如上所述,范围问题是问题所在。以下是我们要解决的问题..

添加了新功能:

$scope.accounts = accounts.data;
  $scope.hasCashAccount = () =>
    $scope.accounts.some(a => a.cardName === 'Cash');

然后将

更改为:

[code]
<p class="disclaimer" ng-if="hasCashAccount()">Cash may be deposited to your card up to 48 hours.</p>
[/code]