Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs中的ng else指令_Javascript_Angularjs_Angular Ng If - Fatal编程技术网

Javascript angularjs中的ng else指令

Javascript angularjs中的ng else指令,javascript,angularjs,angular-ng-if,Javascript,Angularjs,Angular Ng If,我们如何创建与ngIF指令相同的ngELSE指令 以下代码用于ngIfDirective。我们要为ngELSE定制代码吗 var ngIfDirective = ['$animate', function($animate) { return { multiElement: true, transclude: 'element', priority: 600, terminal: true, restrict: 'A', $$tlb: true

我们如何创建与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相反。简单地说
(非)值具有与ng else相同的效果。如果这是您所需要的,那么还有ng else if(调用)指令

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

通常我们这样使用

正常if-else

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

否则,这句话本身就没有多大意义

使用vanilla AngularJS,您可以通过两种方式模仿
else
语句

1.如果需要,只需在第二个ng中使用否定检查

2.使用ngSwitch指令
在本文中,它解释了如何通过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>

有些事情假设“someTest”是真的。
其他一些假设“someTest”是假的东西。


还可以看到:

您的指令不是用来定义使用if和else的流程的,因此我认为没有ngElse。ngif的否定会像ngElse一样。好的,谢谢。现在我要更新我的问题。请看,我知道,伙计,但我想知道,我们能为ngELSE制定一个新的指令吗?是怀疑,是第一个,如果条件是真的,那么else条件将不会执行或执行?@RameshRajendran如果my
if
条件为true,那么您希望在thisng下测试的条件else将调用?@RameshRajendran
ng else
在本例中实际上只是一个否定的
ng if
。所以
ng-then
通过的测试设置为
true
某个测试
true
时,然后
ng-else
基本上检查
!测试通过
<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>
 <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>
<div ng-if='myConditionIsTrue'></div>
<div ng-if='!myConditionIsTrue'></div>
<div ng-switch="myCondition">
    <div ng-switch-when="true"></div>
    <div ng-switch-default></div>
</div> 
<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>