[英]Replace ElementRef with a TemplateRef by condition
I want to replace the button passed as ElementRef
with the template loadingButtonTemplate
if loading
evaluates to true
. 如果
loading
评估为true
我想用模板loadingButtonTemplate
替换作为ElementRef
传递的按钮。 How can I accomplish this? 我该怎么做? If
loading
is back to false
, it should replace vice versa. 如果
loading
恢复为false
,则应反之亦然。
app-form.component.ts: app-form.component.ts:
@Component({
selector: 'app-form',
templateUrl: './form.component.html'
})
export class FormComponent {
@Input()
submitButton: ElementRef;
@Input()
loading: boolean;
@ViewChild('loadingButtonTemplate')
private loadingButtonTemplate: TemplateRef<any>;
constructor() {
}
}
app-form.component.html: app-form.component.html:
<form [formGroup]="formGroup" (ngSubmit)="submit()" autocomplete="off" novalidate>
<ng-content></ng-content>
</form>
<ng-template #loadingButtonTemplate>
<button loadingButton></button> <!-- third party directive -->
</ng-template>
desired usage: 所需用法:
<app-form [submitButton]="submitButton" [loading]="loading">
... some form inputs ...
<button type="button">Delete</button>
<button #submitButton type="submit">Add</button>
</app-form>
Could another, better approach be to "inject" the directive and the needed classes to the button instead of replacing it? 另一种更好的方法是将指令和所需的类“注入”按钮而不是替换它吗?
You can achieve this using *ngIf and else. 您可以使用* ngIf和else实现。
<app-form [submitButton]="submitButton" [loading]="loading">
... some form inputs ...
<button type="button">Delete</button>
<button #submitButton *ngIf="!loading;else loadingButtonTemplate" type="submit">Add</button>
</app-form>
<ng-template #loadingButtonTemplate>
<button loadingButton></button> <!-- third party directive -->
</ng-template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.