我想将一个组件作为子项添加到 elementref 中并可以访问它的属性。
const element: HTMLElement = document.createElement('div');
element.innerHTML = // Component needs to be added here
// Need to have access to properties of the Component here
Run Code Online (Sandbox Code Playgroud)
我在我的项目中添加了一个完整的日历,对于时间线,我需要添加自定义资源元素。为此,我只能访问 HTMLElement 和一些数据。所以我想要做的是将一个初始化的组件传递给 HTMLElement。
现在我唯一的选择是将 html 编写为字符串并将其传递给 innerHTML。有更好的选择吗?
我需要从门户动态附加和分离一组组件的实例,而无需每次附加时都重新初始化它,因为这会大大降低应用程序的性能。
portal = new ComponentPortal(MyComponent);
this.portalHost = new DomPortalHost(
this.elementRef.nativeElement,
this.componentFactoryResolver,
this.appRef,
this.injector
);
const componentRef = this.portalHost.attach(this.portal);
componentRef.instance.myInput = data;
componentRef.instance.myOutput.subscribe(...);
componentRef.changeDetectorRef.detectChanges();
Run Code Online (Sandbox Code Playgroud)
这就是这个问题中解释的方式。但每次重新连接组件时,它都会重新初始化。
对不起,如果这是小事.我创建了一个带有一些子类的抽象类.控制器类创建一个类型为所请求子类类型的抽象类的对象,并返回实现的抽象类.子类具有特定的属性.我无法访问返回对象的那些属性,因为这是抽象类的默认类型,所以我尝试了转换.但这会给出错误"item.Default无法强制转换为item.cloth"
怎么解决?
码:
public class testMain {
public void main(int id) {
Product test;
switch(ProductController.getCategory(id)) {
case "Cloth":
test = (cloth) ProductController.getProduct(id);
break;
case "Wear":
test = (wear) ProductController.getProduct(id);
break;
default:
test = (Default) ProductController.getProduct(id);
}
System.out.println("Product No : " + test.ProductNo);
System.out.println("Title : " + test.Title);
System.out.println("Description : " + test.Desc);
System.out.println("Short Description : " + test.ShortDesc);
System.out.println("Regular Price : " + test.RegularPrice);
System.out.println("Sale Price : " + test.SalePrice);
System.out.println("Category : " + test.Category);
if(((String) test.Category).split(",")[1].contentEquals("cloth")) {
System.out.println("Size : …Run Code Online (Sandbox Code Playgroud)