在Angular 6中,要实现复杂模板,有几种最佳方式可以选择。以下是其中一种方法的示例代码:
ComplexTemplateComponent。import { Component } from '@angular/core';
@Component({
selector: 'app-complex-template',
template: `
复杂模板
加载中...
内容
- {{ item }}
`,
})
export class ComplexTemplateComponent {
isLoading = false;
items = ['Item 1', 'Item 2', 'Item 3'];
addItem() {
this.items.push(`Item ${this.items.length + 1}`);
}
}
在上面的代码中,使用了*ngIf指令根据isLoading属性的值来显示不同的内容。使用ng-template来定义要在条件满足时显示的内容。
通过在模板中添加来插入ComplexTemplateComponent组件。
这是在Angular 6中实现复杂模板的一种最佳方式。根据具体需求,还可以使用其他方法,比如使用动态组件或者使用ng-template来定义可复用的模板。