Tôi muốn thiết lập phần thân của <ng-content>
trong khi khởi tạo thành phần động sử dụng ComponentFactoryResolver
.Tạo thành phần góc 2 với nội dung ng động
Tôi thấy rằng tôi có thể truy cập vào đầu vào & đầu ra sử dụng ComponentRef
, nhưng không phải là cách để đặt <ng-content>
. thành phần
Xin lưu ý <ng-content>
tôi đang lập kế hoạch về thiết lập có thể chứa văn bản đơn giản/SPAN có thể tự động tạo ra
@Component({
selector: 'app-component-to-project',
template: `<ng-content></ng-content>`
})
export class ComponentToProject implements AfterContentInit {
ngAfterContentInit() {
// We will do something important with content here
}
}
@Directive({
selector: 'appProjectionMarker'
})
export class ProjectionMarkerDirective implements OnInit {
constructor(private viewContainerRef: ViewContainerRef, private componentFactoryResolver: ComponentFactoryResolver) {
}
ngOnInit() {
const componentFactory: ComponentFactory<ComponentToProject> = this.componentFactoryResolver.resolveComponentFactory(ComponentToProject);
const componentRef: ComponentRef<ComponentToProject> = this.viewContainerRef.createComponent(componentFactory);
// Question: How to set content before the child's afterContentInit is invoked
}
}
@Component({
selector: 'appTestComponent',
template: `<div appProjectionMarker></div>`
})
export class TestComponent {}
Sử dụng tham số 'projectableNodes' https://stackoverflow.com/questions/41372334/why-is-projectablenodes-an-any – yurzui
Tôi có thể thêm một thành phần động cũng như' projectableNodes', do đó, con có sẵn cho phụ huynh của chỉ thị là '@ ContentChild'? –
Vì nó là một project'Node', tôi cho rằng tôi chỉ có thể chuyển các phần tử 'DOM' –