在ngFor中动态添加组件(Dynamically adding components in ngFor)
我有一个加载配置元素的“仪表板”。 仪表板模板有这样的:
<div class="dash-container" [ngGrid]="gridConfig"> <div *ngFor="let box of boxes; let i = index" [(ngGridItem)]="box.config" (onItemChange)="updateItem(i, $event)" (onResize)="onResize(i, $event)" (onDrag)="onDrag(i, $event)" (onDragStop)="onDragStop(i,$event)" [ngClass]="box.class" > <div class="handle"><h4>{{box.title}}</h4></div> <div [innerHTML]= "box.content"></div> </div> </div>
现在
<div [innerHTML]= "box.content"></div>
将不起作用,因为非标准元素会被清理。 运行最新的Angular 2.4.6(RC 6)。我看看我可以找到的动态组件示例 - 但我看到的只是它们将组件添加到当前组件 - 但我需要它们在上面示例中的特定div中。
ComponentFactoryResolver
经常与@ViewChild
一起使用。 但我不能只在循环内部做这件事:ngAfterViewInit() { const dashWidgetsConf = this.widgetConfigs(); for (var i = 0; i < dashWidgetsConf.length; i++) { const conf = dashWidgetsConf[i]; @ViewChild(conf.id, {read: ViewContainerRef}) var widgetTarget: ViewContainerRef; var widgetComponent = this.componentFactoryResolver.resolveComponentFactory(UnitsComponent); widgetTarget.createComponent(widgetComponent); } }
@viewchild给'装饰者在这里无效'。 我怎样才能从配置列表中加载组件(在一个循环中),并将它们添加到组件中的特定div(divs获得
#{{conf.id}}
)?I have a "dashboard" that loads configured elements. Dashboard template has this:
<div class="dash-container" [ngGrid]="gridConfig"> <div *ngFor="let box of boxes; let i = index" [(ngGridItem)]="box.config" (onItemChange)="updateItem(i, $event)" (onResize)="onResize(i, $event)" (onDrag)="onDrag(i, $event)" (onDragStop)="onDragStop(i,$event)" [ngClass]="box.class" > <div class="handle"><h4>{{box.title}}</h4></div> <div [innerHTML]= "box.content"></div> </div> </div>
Now
<div [innerHTML]= "box.content"></div>
will not work because non standard elements get sanitised. Running latest Angular 2.4.6 (RC 6).I look at the examples i could find for dynamic components - but all i see is that they just add components to the current component - but i need them in a very specific divs like in the example above.
ComponentFactoryResolver
is often used together with@ViewChild
. But i can't just do this inside a loop:ngAfterViewInit() { const dashWidgetsConf = this.widgetConfigs(); for (var i = 0; i < dashWidgetsConf.length; i++) { const conf = dashWidgetsConf[i]; @ViewChild(conf.id, {read: ViewContainerRef}) var widgetTarget: ViewContainerRef; var widgetComponent = this.componentFactoryResolver.resolveComponentFactory(UnitsComponent); widgetTarget.createComponent(widgetComponent); } }
The @viewchild gives 'Decorators are not valid here'. How can i load components from a conf list (in a loop) and add them inside a specific div (divs got
#{{conf.id}}
) in my component?
原文:https://stackoverflow.com/questions/42274281
最满意答案
ByteBuffer.wrap(byteBuffer.array(), <ANYTHING>, <ANYTHING>).array()
表示只是byteBuffer.array()
,并且不考虑<ANYTHING>
。另外,整体
ByteBuffer.wrap(byteBuffer.array(), byteBuffer.position(), byteBuffer.array().length - byteBuffer.position())
只是创建
byteBuffer
的浅表副本的一种繁琐方法,您为什么要这样做而不是仅仅使用byteBuffer
本身?看起来像你想要的是类似的东西
try (FileChannel outCh = new FileOutputStream(filename).getChannel()) { outCh.write(byteBuffer); }
ByteBuffer.wrap(byteBuffer.array(), <ANYTHING>, <ANYTHING>).array()
means justbyteBuffer.array()
, and<ANYTHING>
isn't taken into account.Also, the whole
ByteBuffer.wrap(byteBuffer.array(), byteBuffer.position(), byteBuffer.array().length - byteBuffer.position())
is just a cumbersome way to create a shallow copy of
byteBuffer
, why do you do it instead of just using thebyteBuffer
itself?Looks like what you want is something like
try (FileChannel outCh = new FileOutputStream(filename).getChannel()) { outCh.write(byteBuffer); }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
你应该可以使用Charset.decode(ByteBuffer)来将ByteBuffer转换为CharBuffer 。 然后,只需调用toString() 。 示例代码: import java.nio.*; import java.nio.charset.*; class Test { public static void main(String[] args) throws Exception { byte[] bytes = { 65, 66 }; // "AB" in ASC ...
-
嗯,我不明白你为什么需要对切片缓冲区设置限制。 这应该工作: ByteBuffer originalBuffer = getOriginalBuffer(); ByteBuffer newBuffer = ByteBuffer.allocate(originalBuffer.limit()); ByteBuffer slicedBuffer = originalBuffer.slice(); //Will be from pos to end originalBuffer.flip(); //Will b ...
-
同样来自API规范: 以下循环通过缓冲区buf将字节从一个通道复制到另一个通道: while (in.read(buf) >= 0 || buf.position != 0) { buf.flip(); out.write(buf); buf.compact(); // In case of partial write } Again from the API spec.: The following loop copies bytes from one channel to an ...
-
array()获取原始的underlyimg缓冲区,其中包含一个偏移量可能不是0的FlatBuffer。相反,FlatBufferBuilder中有一个方法(其名称现在让我逃脱),它将为您提供缓冲区。 array() gets the raw underlyimg buffer, which contains the FlatBuffer at an offset possibly other than 0. Instead, there's a method in FlatBufferBuilder (w ...
-
ByteBuffer观点(ByteBuffer views)[2023-05-05]
java.nio中没有实现,但几乎所有通道都实现了GatheringByteChannel和ScatteringByteChannel ,它们可以直接读写ByteBuffers数组。 GatheringByteChannel : long write(ByteBuffer[] srcs) 从给定的缓冲区向该通道写入一个字节序列。 和ScatteringByteChannel : 长read(ByteBuffer[] dsts) 从该通道读取一系列字节到给定的缓冲区。 还有上述方法的部分版本,请参阅上面的链接 ... -
ByteBuffer.wrap(byteBuffer.array(),
, ).array()表示只是byteBuffer.array() ,并且不考虑 。 另外,整体 ByteBuffer.wrap(byteBuffer.array(), byteBuffer.position(), byteBuffer.array().length - byteBuffer.position()) 只是创建byteBuffer的浅表副本的一种繁琐方法,您为什 ... -
每次从写入到读取以及从读取到写入交换时,都有flip()缓冲区。 在receive()之后将flip()添加到第二个示例 一旦你有了这个工作,你应该看看如何重用你的ByteBuffers和EJP建议。 You have th flip() the buffer each time you swap from write to read and from read to writing. Add flip() to the second example after receive() Once you hav ...
-
这就是我解决它的方式。 如果它期望ByteBuffer让它给它ByteBuffer。 我将功能更改为: private byte[] buildAvroData(String name, byte[] data) throws IOException { out.reset(); GenericRecord datum=new GenericData.Record(avroSchema); datum.put("name", name); da ...