6

我们如何创建ngELSE与指令相同的ngIF指令?

下面的代码ngIfDirective。我们要自定义代码ngELSE吗?

var ngIfDirective = ['$animate', function($animate) {
  return {
    multiElement: true,
    transclude: 'element',
    priority: 600,
    terminal: true,
    restrict: 'A',
    $$tlb: true,
    link: function($scope, $element, $attr, ctrl, $transclude) {
        var block, childScope, previousElements;
        $scope.$watch($attr.ngIf, function ngIfWatchAction(value) {

          if (value) {
            if (!childScope) {
              $transclude(function(clone, newScope) {
                childScope = newScope;
                clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
                // Note: We only need the first/last node of the cloned nodes.
                // However, we need to keep the reference to the jqlite wrapper as it might be changed later
                // by a directive with templateUrl when its template arrives.
                block = {
                  clone: clone
                };
                $animate.enter(clone, $element.parent(), $element);
              });
            }
          } else {
            if (previousElements) {
              previousElements.remove();
              previousElements = null;
            }
            if (childScope) {
              childScope.$destroy();
              childScope = null;
            }
            if (block) {
              previousElements = getBlockNodes(block.clone);
              $animate.leave(previousElements).then(function() {
                previousElements = null;
              });
              block = null;
            }
          }
        });
    }
  };
}];
4

4 回答 4

6

通常我们这样使用

正常的 if-else

if(video == video.large){
    <!-- code to render a large video block-->
}
else{
    <!-- code to render the regular video block -->
}

AngularJS ng-if

<div ng-if="video == video.large">
    <!-- code to render a large video block-->
</div>
<div ng-if="video != video.large">
    <!-- code to render the regular video block -->
</div>

但是如果你太具体了,你想要一个像ng-if,这样的指令ng-else-ifng-else然后使用ng-elif

工作演示

 <div ng-if="someCondition">
    ...
  </div>
  <p>
    Some random junk in the middle.
  </p>
  <div ng-else-if="someOther && condition">
    ...
  </div>
  <div ng-else-if="moreConditions">
    ...
  </div>
  <div ng-else>
    ...
  </div>
于 2015-04-24T06:31:20.713 回答
5

En else 语句本身没有多大意义。

您可以else使用 vanilla AngularJS 以 2 种方式模仿语句

1. 只需在第二个 ng-if 中使用否定检查

<div ng-if='myConditionIsTrue'></div>
<div ng-if='!myConditionIsTrue'></div>

2.使用ngSwitch指令

<div ng-switch="myCondition">
    <div ng-switch-when="true"></div>
    <div ng-switch-default></div>
</div> 
于 2015-04-24T06:32:58.583 回答
1

这样做,它与 ng-if 相反。简单地说!(NOT) Value 与 ng-else 具有相同的效果。还有 ng-else-if (称为ng-elif)指令,如果这更多的是你正在寻找的。

<div ng-controller="myCtrl as ctrl">
    <div ng-if="ctrl.isTrue">If</div>
    <div ng-if="!ctrl.isTrue">If</div>
</div>

虽然当您可以简单地否定 ng-if 中的检查值时创建 ng-else 指令实际上没有意义,但您可以像这样修改 ng-if 指令以实现完全相同的事情

    $scope.$watch($attr.ngIf, function ngIfWatchAction(value) {

      if (!value) { // add the ! here instead and name this new directive ngElse
于 2015-04-24T06:29:46.463 回答
1

在这里,它解释了如何通过 ng-elif 使用 ng-else

例子:

<div ng-if="someTest" ng-then="theTestPassed">
  Some things that assume that "someTest" is true.
</div>
<div ng-else="theTestPassed">
  Some other things that assume that "someTest" is false.
</div>

http://zachsnow.com/#!/blog/2014/angularjs-ng-elif/

另见:http ://plnkr.co/edit/XSPP3jZL8eehu9G750ME?p=preview

于 2015-04-24T06:34:56.010 回答