angular - 如何在 Angular 6 中将数据从父组件绑定到可重用组件?
解决方案
您可以在指令中通过 @Input() 以角度将数据传递给指令:
import { Directive, ViewContainerRef, OnInit , Input } from '@angular/core';
import { ReusableService } from './reusable.service';
@Directive({
selector: '[reusableOutlet]'
})
export class ReusableDirective implements OnInit {
@Input()
set reusableOutlet(passedValue: any) {
console.log(passedValue);
}
constructor(private viewContainerRef: ViewContainerRef, private reusableService: ReusableService) {}
public ngOnInit(): void {
this.reusableService.attach(this.viewContainerRef);
}
}
用法:
<ng-template [reusableOutlet]="data"></ng-template>
演示。
推荐阅读
- python - Unexpected keyword argument 'ragged' in Keras
- java - 如何在 Android 应用程序的两个模块之间传递变量(本地语言)?
- python - 尝试使用 opencv 的 fillPolly 将多边形添加到图像上时出错
- google-cloud-platform - Cloud Pub/Sub 上未确认的消息
- c++ - VB.NET DLL中的C++ DLL函数复制
- reactjs - 将主组件状态传递给另一个组件错误
- outlook - 在 Outlook 中按“发送”按钮后如何添加通知窗口?
- spring-boot - 如何在 groovy 中使用 Spring 表达式语言(SpEL)?(每个春季调度程序crontab)
- angular - 需要在页面之间维护表单数据,而无需在 angular-ngrx 页面中向服务器/api 发送数据
- c# - 如何在父类的自定义用户控件中设置自定义验证错误模板的属性