javascript-Angular中的createEmbeddedView()上下文参数是什么
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-Angular中的createEmbeddedView()上下文参数是什么,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2034字,纯文字阅读大概需要3分钟。
内容图文
我想知道上下文参数在Angular中的createEmbeddedView()方法中起什么作用.在线的角度文档不提供此信息.
例如,我在作者编写迭代器结构指令的地方阅读此代码.
import {
Directive, ViewContainerRef, TemplateRef, Input, SimpleChange
} from "@angular/core";
@Directive({
selector: "[paForOf]"
})
export class PaIteratorDirective {
constructor(private container: ViewContainerRef, private template: TemplateRef<Object>) {
}
@Input("paForOf") dataSource: any;
ngOnInit() {
this.container.clear();
for (let i = 0; i < this.dataSource.length; i++) {
this.container.createEmbeddedView(this.template, new PaIteratorContext(this.dataSource[i]));
}
}
}
class PaIteratorContext {
constructor(public $implicit: any) { }
}
这样显示在模板上的复选框已选中事件中,如下所示:
<div class="checkbox">
<label><input type="checkbox" [(ngModel)]="showTable" />Show Table</label>
</div>
<table *paIf="showTable" class="table table-sm table-bordered table-striped">
<tr>
<th></th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
<template [paForOf]="getProducts()" let-item>
<tr>
<td colspan="4">{{item.name}}</td>
</tr>
</template>
</table>
我想了解以下代码:
this.container.createEmbeddedView(this.template, new PaIteratorContext(this.dataSource[i]));
为什么我需要创建一个PaIteratorContext()类的对象?为什么我不能做到这一点:
this.container.createEmbeddedView(this.template, this.dataSource[i]);
请帮忙 ?
解决方法:
定义模板时,可以通过let-paramname指定输入参数:
<template [paForOf]="getProducts()" let-item='item'>
<span>{{item.name}}</span>
</template>
上下文对象是允许您在创建模板时将这些参数传递给模板的对象.
this.container.createEmbeddedView(this.template, {item: {name: 'John'}}`
Why i need to create an object of PaIteratorContext() class? why i
can’t just do this:
您不必创建PaIteratorContext的实例,只需要在特定情况下传递带有name属性的对象即可.因此,以下内容也将起作用:
this.container.createEmbeddedView(this.template, { $implicit: this.dataSource[i] });
如果像这样的let-item那样指定了输入属性,而没有第二部分= something,则嵌入式视图将其视为let-item = $implicit,因此您必须传递带有$implicit属性的上下文对象.
内容总结
以上是互联网集市为您收集整理的javascript-Angular中的createEmbeddedView()上下文参数是什么全部内容,希望文章能够帮你解决javascript-Angular中的createEmbeddedView()上下文参数是什么所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。