javascript - Bootstrap popover 在 *ngIf 内以角度失败
问题描述
我正在尝试在容器内显示弹出框,但它不起作用,
如果我删除 *ngIf 它可以工作
如果 *ngIf 不渲染,在哪里
<div class="container" *ngIf="data" >
<button
class="popover"
data-trigger="hover"
data-toggle="tooltip"
data-content="hello"
data-container="body">
<mat-icon>
info
</mat-icon>
</button>
</div>
//ts文件
export class SomeComponent implements OnInit {
//... variables
//... constructor
ngOnInit() {
$('.popover').popover({
boundary: 'viewport',
placement: 'top',
container:'body',
sanitize: true,
appendToBody: true
})
}
}```
解决方案
这不起作用,因为当 ngOnInit() 被 Angular 调用时,div.container 不是由 Angular 渲染的。相反,您可以使用 AfterViewInit Lifecycle 挂钩,如下所示。
注意带有ngIf的段落不能在ngOnInit中加载,但可以在ngAfterViewInit中加载。
Stackblitz 代码:https ://stackblitz.com/edit/angular-ngif-lifecycle-hook
组件.html
<p id="p1">
Without ngIf
</p>
<p id="p2" *ngIf="data">
With ngIf
</p>
组件.ts
import { Component, VERSION, OnInit, AfterViewInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit, AfterViewInit{
data = {random: 'text'};
ngOnInit() {
const withoutNgIf = document.getElementById('p1');
const withNgIf = document.getElementById('p2');
console.log('OnInit without ngIf: ', withoutNgIf);
# Output: HTMLParagraphElement
console.log('OnInit with ngIf: ', withNgIf);
# Output: null
}
ngAfterViewInit() {
const withNgIf = document.getElementById('p2');
console.log('AfterViewInit with ngIf: ', withNgIf);
# Output: HTMLParagraphElement
}
}
我希望这可以帮助您理解问题。
提示:如果您使用 angular,我建议使用 ViewChild 装饰器来访问 DOM 而不是 jquery。(例如:https ://dev.to/danielpdev/how-to-use-viewchild-decorator-in-angular-9-i0 )
推荐阅读
- ruby-on-rails - 安装后不显示ckeditor
- azure-cosmosdb - CosmosDB 的 Gremlin - 无法在非原始类型 GraphTraversal 上创建 ValueField
- python-3.x - Facebook Messenger bot 的日期选择器 webview - 无法将字段值带回 bot 的输入字段
- php - 检查点防火墙实施后的会话交换
- java - FileReader 和 ActionListner 之间的同步问题,FileReader 在我可以给他任何勇气之前给出空指针;
- apache-spark - 从长转换为时间戳以插入数据库
- docker - 从 jwilder/nginx-proxy 提供静态文件
- xslt - xsl:text 元素中的空格
- mysql - SQL:如何通过连接从不同的表中获取多行?(初学者的问题)
- dialogflow-es - 在谷歌助手中更改表格的背景颜色