- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了了解我的问题,这里有一个关于正在发生的事情和没有发生的事情的解释:
我有上传 .zip 文件的上传功能,获取所有 html 代码、图像并将其转换为字符串。对于每次上传,它都会重复,之后当我加载该 .zip 文件时,它总是会提取它并将其所有内容转换为字符串 -> 然后我将其放入 .zip 文件中。问题是它只能工作一次(将字符串放入 iframe)。
它的工作原理如下:
我获取 zip 文件并调用 ShowVisualContent() 将其所有内容转换为字符串。所有字符串都在我的 navService -> this.html: string;
this.uploads = this.db.list(`profile/${this.auth.userId}/projects/${this.projectId}`).snapshotChanges().map((actions) => {
return actions.map((a) => {
const data = a.payload.val();
this.navSrv.showVisualContent(data.url, data.name);
const $key = a.payload.key;
const $ref = a.payload.ref;
return { $key, ...data, $ref };
});
});
然后在我的组件中我得到了 html:string;
@ViewChild('html') html: ElementRef;
loadHtml(){
setTimeout(()=> {
this.html.nativeElement.src = 'data:text/html,' + encodeURIComponent(this.navSrv.html);
},4000);
console.log("Loading html into iframe now")
}
并将其加载到 html 中,如下所示:
<div *ngFor="let upload of uploads | async">
<iframe style="min-width:1300px;min-height:800px;" id="framex" #html scrolling="no" frameborder="0"></iframe>
</div>
主要问题是,当我在一个页面中加载大量 zip 文件时,我只得到 1 个包含转换后的 html 字符串的文件。
如何使 this.html 成为全部而不仅仅是一个。
如何从每个函数调用中获取数组形式的值:
html: string;
this.html = xmlDoc.documentElement.innerHTML;
(这是我将所有 html 放入 this.html 字符串的部分,它只得到最后一个值。)
如何以数组形式获取所有 html?
这是我获取所有 .zip 文件的 innerHTML 的函数:
html:string;
public showVisualContent(contentUrl:string, fileName:string) {
console.log(contentUrl);
console.log(fileName);
let fileInfo = getFileInfo(fileName);
if(fileInfo) {
switch(fileInfo.type) {
case 'archive' : {
getAllFileContentsFromRemoteZip(contentUrl, (files) => {
//TODO: Remove timeouts. This is just a temporary option for demonstration purposes.
//Also this is a massive function that must be separated into multiple functions
setTimeout(() => {
let storageRef = firebase.storage().ref();
for(let i = 0; i < files.length; i++) {
if(files[i].fileInfo.type == 'image') {
// Create a storage reference from our app
// Create a reference with an initial file path and name
let imageRef = storageRef.child(files[i].fileInfo.fileName);
files[i].url = imageRef.getDownloadURL().then((url) => {
return url;
});
}
}
setTimeout(() => {
for(let i = 0; i < files.length; i++) {
//console.log(files[i].fileInfo.fileName);
//console.log(files[i].url);
if(files[i].fileInfo.type == 'web') {
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(files[i].content, "text/html");
//scripts
let scriptElements = xmlDoc.getElementsByTagName('script');
for(let j = 0; j < scriptElements.length; j++) {
let attr = scriptElements[j].getAttribute('src');
if(attr) {
for(let k = 0; k < files.length; k++) {
if(attr.includes(files[k].fileInfo.fileName)) {
scriptElements[j].removeAttribute('src');
scriptElements[j].innerHTML = files[k].content;
}
}
}
}
//styles
let linkElements = xmlDoc.getElementsByTagName('link');
for(let j = 0; j < linkElements.length; j++) {
if(linkElements[j].getAttribute('rel') == 'stylesheet')
{
let attr = linkElements[j].getAttribute('href');
if(attr) {
for(let k = 0; k < files.length; k++) {
if(attr.includes(files[k].fileInfo.fileName)) {
//do stuff
let parentElement = linkElements[k].parentElement;
if(parentElement) {
let styleElement = parentElement.appendChild(xmlDoc.createElement('style'))
styleElement.innerHTML = files[k].content;
}
}
}
}
}
}
//images
let imgElements = xmlDoc.getElementsByTagName('img');
for(let j = 0; j < imgElements.length; j++) {
let attr = imgElements[j].getAttribute('src');
if(attr) {
for(let k = 0; k < files.length; k++) {
if(attr.includes(files[k].fileInfo.fileName)) {
//do stuff
//imgElements[k].setAttribute('src', 'data:image/' + files[k].fileInfo.ext + ';base64,' + files[k].content);
imgElements[k].setAttribute('src', files[k].url.i);
}
}
}
}
//console.log(xmlDoc.documentElement.innerHTML);
this.html = xmlDoc.documentElement.innerHTML;
for(let j = 0; j < files.length; j++) {
if(files[j].fileInfo.type == 'image') {
let strings = getStringsToReplace(files[j].fileInfo.fileName, this.html);
for(let k = 0; k < strings.length; k++) {
this.html = this.html.replace(strings[k], files[j].url.i);
this.html = this.html.replace('/' + strings[k], files[j].url.i);
}
}
}
// console.log(this.html);
}
}
}, 500);
}, 1000);
});
}
case 'web' : {
//show simple html here. Unlikely to happen
}
case 'image' : {
//show image here
}
}
}
}
最佳答案
ViewChild 的设计为您提供一个实例,使用 viewChildren :
@ViewChildren('html') htmls: QueryList<ElementRef>;
loadHtml(){
setTimeout(()=> {
this.htmls.map((elem) => {
elem.nativeElement.src = 'data:text/html,' + encodeURIComponent(this.navSrv.html);
}
},4000);
console.log("Loading html into iframe now")
}
为了更好地理解ViewChild & ViewChildren,queryList查看这篇文章https://netbasal.com/understanding-viewchildren-contentchildren-and-querylist-in-angular-896b0c689f6e
关于javascript - Angular 5 viewchild 只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50187424/
我目前正在学习 angular ViewChild,但在使用 ViewChild 从 DOM 选择自定义引用变量时,我似乎遗漏了一些东西。 在我的模板中有两个表: 我正在尝试针对每个对象进行排序:
在我的组件中,我有以下内容: @ViewChild('PaginatedTableComponent') userTable: PaginatedTableComponent; 在这个组件中,我有以下
我正在使用父子组件。在子组件中,我使用搜索框。我需要随时按需获取搜索框值。如何获得? 子组件 html - GridViewComponent 父组件 ts @ViewChild(GridviewC
我有两个组件,一个 videoComponent 和 videoControlsComponent。视频组件包含 元素和视频组件有一些按钮来操作 videoComponent 元素。 You
我想在我的 HTML 中打印一个 10x10 的网格。为此,我在我的 html 中使用了两个 ngFor。在我的 TypeScript 组件中,我想使用 @ViewChild 访问此表的单元格以将光标
在我的 Angular 应用程序中,我的 @ViewChild 实例无法填充 HTL matSort。 我的组件.ts: import { MatSort } from '@angular/mater
基本上不是这样做: class Controller implements AfterViewInit{ @ViewChild(IconComponent) iconComponent: Ic
我已经检查了这个问题的其他答案,但到目前为止似乎没有任何帮助。我正在尝试在 ionic 2 应用程序中显示 map 。但是当我尝试选择 #map 时div 它返回未定义。这是代码 page1.html
出于某种原因,我的 Angular 5 应用程序中的 @ViewChild 无法正常工作。我在我的组件中这样定义它: case-detail.component.html: 我在我的 C
我有这样一个组件: export class ParentComponent implements OnInit, OnDestroy { @ViewChild(ChildComponent)
我有一个名为 EasyBoxComponent 的组件,和一个带有此 @Viewchild 的 @Directive: @ViewChild(EasyBoxComponent) myComponent
需要帮助来理解以下语句中 {read: ViewContainerRef} 的含义。 @ViewChild('myname', {read: ViewContainerRef}) target; 最佳
在以前的angular版本中,我们可以像这样定义一个带有元素ref的viewchild @ViewChild('fileInput') fileInput: ElementRef; 现在我们必须在构造
Angular v8 刚刚发布。虽然它主要是向后兼容的,但还是有一些突破性的变化。 根据 Angular 的 Changelog一个核心变化是(我引用): “在 Angular 版本 8 中,要求所有
我在使用 @ViewChild 和通过 ngIf 显示的组件时遇到问题。我找到了各种解决方案,但没有一个对我有用。 这是我的主要组件,由多个步骤(为了简洁起见,我只在代码中显示 2 个)和一个用于向前
我正在尝试获取 audio组件内的元素。起初我是用老式的方式做的: $player: HTMLAudioElement; ... ngOnInit() { this.$player = docum
这个问题已经有答案了: What are all the valid selectors for ViewChild and ContentChild? (2 个回答) 已关闭 4 年前。 在本文中C
如果我的 fileInput 元素在一个普通的 div 中,那么它工作正常。但是如果我把它放在 里面然后我得到它未定义。 这是我的代码: HTML Modal title
我目前正在开发一个项目,我需要根据组件传递一些@input值。因此,该指令将相应地发挥作用。但问题是我无法获得该指令的引用。相反,我只得到了 elementRef 作为返回。请参阅下面的 stackb
我是 angular2 的新手,并不专业。我有一个父子组件,当用户单击按钮时,我希望子组件的选择器显示一些内容。所以它应该是条件,并且它也是从子组件中使用的。这是我的代码:父级 HTML:
我是一名优秀的程序员,十分优秀!