angular - Angular 如何在动态组件中使用@decorators?
问题描述
基本上,我需要基于动态模板创建组件,并将父数据共享给新创建的组件,并将数据共享给其他组件。
// import
@Component({
selector: 'app-modal',
template: `<div #vcf></div>`,
providers: [AppsService],
encapsulation: ViewEncapsulation.None,
})
export class AppModalComponent implements OnInit, OnDestroy, AfterViewInit {
@ViewChild('vcf', {read: ViewContainerRef}) vcf: ViewContainerRef;
private cmpRef: ComponentRef<any>;
constructor(
private appService: AppsService,
private compiler: Compiler,
private injector: Injector,
private m: NgModuleRef<any>
) {}
ngOnInit() { }
ngAfterViewInit() {
let template;
const styles = [`input { width: 100px }`];
this.appService.getAppTemplate().subscribe((templateData) => {
console.log('dynamic html data ::: ', templateData); // ok
template = templateData;
// Component dynamically.
const templateComponent = Component({template})(class DynamicTemplateComponent {
@Input() sendSMS; // Error, Saying Decorators are not valid here
someMethod() {
alert('some data submitting'); // working
}
addSMSData() {
alert('ok');
}
});
const templateModule = NgModule({
imports: [RouterModule, FormsModule, CommonModule],
declarations: [templateComponent]
})(class {});
this.compiler.compileModuleAndAllComponentsAsync(templateModule)
.then((factories) => {
const f = factories.componentFactories[0];
this.cmpRef = f.create(this.injector, [], null, this.m);
this.cmpRef.instance.name = 'templateFrm';
this.vcf.insert(this.cmpRef.hostView);
});
});
}
}
这是我正在渲染和工作的虚拟模板。
<input type="text" id="sms_title" [(ngModel)] ="sms_title" name="sms_title" placeholder="SMS title" required> <button type="submit" (click)="addSMSData()">Submit</button>
那么如何在新创建的组件中使用装饰器,它拥有一切动态。
@Input() 获取短信数据;// 错误,装饰器在这里无效
一些想法将是可观的。
谢谢。
解决方案
推荐阅读
- javascript - 如何限制 JS 的不透明度有多大
- asp.net-core - Aspose Cell .net core:Linux主机上的一些文本被截断
- c# - 如何为输入变量值简化此 Switch 语句?
- matlab - 如何线性索引蒙版图像
- asp.net-core - 在启动时使用 IOptions 进行 aspnet 核心依赖注入
- python - 在 Python 中使用 itertools 时出错
- c - 无法正确使用 C 中的 scanf() 将字符串作为结构的输入
- php - 作曲家供应商公开副本没有为我的 silverstrip 模块选择正确的名称
- mysql - 在查询字符串中传递多个 id,并在节点 js 中的 mysql 中为它们获取数据
- python - 如何解决:摆脱 \n'] 会破坏我的输出?