angularjs 中的 ng-else 指令

ng-else directive in angularjs

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

下面是 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;
            }
          }
        });
    }
  };
}];

做这个,它是 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

通常我们这样使用

正常 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-ifng-else-ifng-else 这样的指令,那么使用 ng-elif

Working Demo

 <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>

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

您可以通过两种方式使用原版 AngularJS

模仿 else 语句

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> 

在此解释了如何通过 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