我使用的是角度4,我有一个指令需要在模板上添加以验证一些内容。但是我想首先评估是否有其他条件成立,然后再应用该指令。现在我的代码是这样的:
<div *ngIf="groupCheck; else NoDirective" #Directive>
<li [directive]="controlGroup"><a [routerLink]="['/account/search/','']"
routerLinkactive="active">accounts</a></li>
<li [directive]="controlGroup"><a [routerLink]="['/orders','']"
routerLinkactive="active">Orders</a></li>
<li [directive]="DIFFERENT_GROUP"><a [routerLink]="['/report']" routerLinkactive="active">Reports</a>
</li>
<li [directive]="controlGroup"><a [routerLink]="['/anotherStuff']" routerLinkactive="active">anotherStuff</a></li>
</div>
<div *ngIf="!groupCheck; else Directive" #NoDirective>
<li><a [routerLink]="['/account/search/','']" routerLinkactive="active">accounts</a></li>
<li><a [routerLink]="['/orders','']" routerLinkactive="active">Orders</a></li>
<li><a [routerLink]="['/report']" routerLinkactive="active">Reports</a></li>
<li><a [routerLink]="['/anotherStuff']" routerLinkactive="active">anotherStuff</a></li>
</div>
我想找到一种方法来做这样的事情:
<li *NgIf="condition true add [directive]=controlGroup; else Don't add directive to this line/tag/element"><a [routerLink]="['/account/search/','']"
routerLinkactive="active">accounts</a></li>
<li *NgIf="condition true add [directive]=controlGroup; else Don't add directive to this line/tag/element"><a [routerLink]="['/orders','']"
routerLinkactive="active">Orders</a></li>
<li *NgIf="condition true add [directive]=DIFFERENTGROUP; else Don't add directive to this line/tag/element"><a [routerLink]="['/report']" routerLinkactive="active">Reports</a>
</li>
<li *NgIf="condition true add [directive]=controlGroup; else Don't add directive to this line/tag/element"><a [routerLink]="['/anotherStuff']" routerLinkactive="active">anotherStuff</a></li>
这样一来,我将不必只为一个条件重写整个代码,也不需要条件div。有办法吗?
----- ******更新****** ----- @Allabakash在帖子中为我提供了可能的解决方案:
<button [attr.md-raised-button]="condition ? '' : null"></button>
我现在的问题是我的指令(我无法访问)消除了整个元素(如果它为null或方法中未出现的名称)。运作方式如下:
set checkGroup(hasGroups: string) {
if (hasGroups) {
this.AuthService.hasOtherGroups(hasGroups,false).subscribe(res => {
if (!res) {
this.el.nativeElement.parentNode.removeChild(this.el.nativeElement);
} else {
this.el.nativeElement.style.display = '';
}
});
} else {
this.el.nativeElement.parentNode.removeChild(this.el.nativeElement);
}
}
哪个让我留有一个大问号:有没有一种方法可以将该指令与
感谢您的时间:)。