0

我试图将 ngRepeat 与 ngIf 或 ngShow 一起使用。它只是行不通。我试图只显示匹配“海报”和“查看者”ID的那些,这样用户只会看到他/她自己的帖子。我使用meanJS。我正在研究指令,因为我不知道这是否是我所缺少的。

<div class="col-xs-12 col-sm-4 col-md-3 col-lg-3 extraSpaceList text-center" data-ng-repeat="motor in motorsCtrl.motors | filter:searchText | filter:searchFilter">
        <div ng-if="((authentication.user) && (authentication.user._id == motor.user._id))">
        <a class="list-group-item" ng-click="motorsCtrl.modalUpdate('lg', motor)">
            <div class="bg-info">
                <h4 class="list-group-item-heading" data-ng-bind="motor.title"></h4>
            </div>
        </a>
        </div>
    </div>
4

1 回答 1

1

A couple of things to look into.

First:

If you are using ng-if, ng-if will create its own scope. You cannot access the parent scope model from within ng-if unless you use the $parent service. So you will need to change your models: motorsCtrl.modalUpdate('lg', motor) becomes $parent.modalUpdate('lg', motor) and motor.title becomes $parent.motor.title

Second:

You should probably start out using ng-show instead of ng-if here. At least until your app renders a view. But look into it.

You may want to consider the following (taken from @markovuksanovic):

ng-if will remove elements from DOM. This means that all your handlers or anything else attached to those elements will be lost. For example, if you bound a click handler to one of child elements, when ng-if evaluates to false, that element will be removed from DOM and your click handler will not work any more, even after ng-if later evaluates to true and displays the element. You will need to reattach the handler. ng-show/ng-hide does not remove the elements from DOM. It uses CSS styles to hide/show elements (note: you might need to add your own classes). This way your handlers that were attached to children will not be lost. ng-if creates a child scope while ng-show/ng-hide does not

于 2015-01-04T12:03:23.727 回答