Javascript angularjs中的ng else指令
我们如何创建与ngIF指令相同的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
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如果myif
条件为true,那么您希望在thisng下测试的条件else将调用?@RameshRajendranng 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>