【发布时间】:2017-08-23 18:43:31
【问题描述】:
我有角度组件
在 component.ts 我有一行代码可以搜索 html 元素
var myArticle = document.querySelector('article');
这个返回空
在component.html中
<article id="bodyArticle" *ngIf="isClicked"></article>
但是当我在兄弟component.ts中使用文章时
var myArticle != null from component.ts
所有文件项目中的querySelector serach元素是如何工作的?
我还有其他问题。在同一个 componen.html 我有按钮
<div class="btnGrp">
<button (click)="loadXML($event)">Load</button>
<input type="file" name="img" multiple (change)="onChange($event)">
</div>
<article id="bodyArticle" *ngIf="isClicked"></article>
当我单击按钮时,发出的一次单击首先是值为真,我必须单击第二个按钮才能将其加载到文章内容中
来自component.ts的sn-p
loadXML(event?: Event) {
var myArticle = document.querySelector('article');
console.log(myArticle);
if(this.imageService.getBodyRes() == ''){
myArticle.textContent = 'Error can't load data';
this.isClicked = true;
this.xmlLoadedEvent.emit(this.isClicked);
} else {
myArticle.textContent = this.imageService.getBodyRes();
console.log(myArticle.textContent);
this.isClicked = true;
this.xmlLoadedEvent.emit(this.isClicked);
}
}
当我单击文章标签 ngIf 中的按钮设置 true 值并且还加载数据时,如何做到这一点,而无需单击。
【问题讨论】:
-
我们需要更多的描述。我相信您在组件安装到 DOM 之前调用了选择器。
-
@squgeim 我认为错误是 isclicked = false 并且元素尚未渲染并返回空值
-
如果你的同级组件
在文章之后加载,那就是问题 -
你什么时候调用你的方法,
isClicked一定是真的,是吗? -
打开你的控制台并运行
document.querySelector('article');看它是否返回null
标签: javascript html angular