Ale*_*ano 3 javascript angularjs angular-directive ng-template angular
所以我想知道是否有办法传递 ng-template 并生成它的所有内容以包含插值中使用的变量?
另外,我还是 angular 的新手,所以除了删除 html 元素之外,我还需要担心删除其他任何内容吗?
最后会有一个指向 stackblitz.com 存储库的链接,其中包含下面显示的所有代码。
以下是实现我的指令的 src/app/app.component.html 代码:
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<!-- popup/popup.directive.ts contains the code i used in button tag -->
<button PopupDir="" body="this is a hardcoded message that is passed to popup box"> simple
</button>
<ng-template #Complicated="">
<div style="background-color: red;">
a little more complicated but simple and still doable
</div>
</ng-template>
<button PopupDir="" [body]="Complicated">
complicated
</button>
<ng-template #EvenMoreComplicated="">
<!-- name and data isn't being passed i need help here-->
<div style="background-color: green; min-height: 100px; min-width:100px;">
{{name}} {{data}}
</div>
</ng-template>
<button PopupDir="" [body]="EvenMoreComplicated">
more complicated
</button>
Run Code Online (Sandbox Code Playgroud)
以下是我的 src/app/popup/popup.directive.ts
import { Directive, Input, TemplateRef, ViewContainerRef, HostListener } from '@angular/core'
@Directive({
selector: 'PopupDir, [PopupDir]'
})
export class Popup {
@Input() body: string | TemplateRef<any>;
viewContainer: ViewContainerRef;
popupElement: HTMLElement;
//i dont know if i need this
constructor (viewContainer: ViewContainerRef) {
this.viewContainer = viewContainer;
}
//adds onlick rule to parent tag
@HostListener('click')
onclick () {
this.openPopup();
}
openPopup() {
//Pcreate pupup html programatically
this.popupElement = this.createPopup();
//insert it in the dom
const lastChild = document.body.lastElementChild;
lastChild.insertAdjacentElement('afterend', this.popupElement);
}
createPopup(): HTMLElement {
const popup = document.createElement('div');
popup.classList.add('popupbox');
//if you click anywhere on popup it will close/remove itself
popup.addEventListener('click', (e: Event) => this.removePopup());
//if statement to determine what type of "body" it is
if (typeof this.body === 'string')
{
popup.innerText = this.body;
} else if (typeof this.body === 'object')
{
const appendElementToPopup = (element: any) => popup.appendChild(element);
//this is where i get stuck on how to include the context and then display the context/data that is passed by interpolation in ng-template
this.body.createEmbeddedView(this.viewContainer._view.context).rootNodes.forEach(appendElementToPopup);
}
return popup;
}
removePopup() {
this.popupElement.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
这是显示我的问题的回购链接:https : //stackblitz.com/edit/popupproblem
首先让我们考虑如何将上下文传递给嵌入式视图。你写了:
this.body.createEmbeddedView(this.viewContainer._view.context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
您的Popup组件托管在AppComponent视图中,因此this.viewContainer._view.context将是AppComponent实例。但我要你说的是:
1) 嵌入式视图已经可以访问ng-template定义的模板范围。
2)如果我们传递上下文,那么它应该只通过模板引用变量使用。
this.body.createEmbeddedView(this.viewContainer._view.context)
||
\/
this.body.createEmbeddedView({
name = 'Angular';
data = 'this should be passed too'
})
||
\/
<ng-template #EvenMoreComplicated let-name="name" let-data="data">
{{name}} {{data}}
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下你不需要传递上下文,因为它已经存在了。
this.body.createEmbeddedView({})
||
\/
<ng-template #EvenMoreComplicated>
{{name}} {{data}}
Run Code Online (Sandbox Code Playgroud)
角度变化检测机制依赖于视图树。
AppComponent_View
/ \
ChildComponent_View EmbeddedView
|
SubChildComponent_View
Run Code Online (Sandbox Code Playgroud)
我们看到有两种视图:组件视图和嵌入视图。TemplateRef(ng-template) 表示嵌入视图。
当 Angular 想要更新 UI 时,它只需通过该视图的两个检查绑定。
现在让我们提醒我们如何通过低级 API 创建嵌入式视图:
TemplateRef.createEmbeddedView
ViewContainerRef.createEmbeddedView
它们之间的主要区别在于,前者只是创建 EmbeddedView 而后者创建 EmbeddedView并将其添加到 Angular 更改检测树中。通过这种方式,嵌入式视图成为变更检测树的一部分,我们可以看到更新的绑定。
是时候看看你的代码了:
this.body.createEmbeddedView(this.viewContainer._view.context).rootNodes.forEach(appendElementToPopup);
Run Code Online (Sandbox Code Playgroud)
很明显,您正在使用第一种方法。这意味着您必须自己处理更改检测:viewRef.detectChanges()手动调用或附加到树。
简单的解决方案可能是:
const view = this.body.createEmbeddedView({});
view.detectChanges();
view.rootNodes.forEach(appendElementToPopup);
Run Code Online (Sandbox Code Playgroud)
但它只会检测一次变化。我们可以detectChanges在每个Popup.ngDoCheck()钩子上调用方法,但 Angular 本身使用了一种更简单的方法。
const view = this.viewContainer.createEmbeddedView(this.body);
view.rootNodes.forEach(appendElementToPopup);
Run Code Online (Sandbox Code Playgroud)
我们使用了第二种创建嵌入视图的方法,这样模板会被 Angular 本身自动检查。
我还是 angular 的新手,所以除了删除 html 元素之外,我还需要担心删除其他任何内容吗?
我认为我们也应该在关闭弹出窗口时销毁嵌入式视图。
removePopup() {
this.viewContainer.clear();
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3508 次 |
| 最近记录: |