admin管理员组文章数量:1241068
I have a small issue that I can't figure out how to do. I am just learning Angular / Typescript and I can't figure out how I could remove some DOM elements. I have some auto generated content, that has specific CSS classes. Unfortunately The objects are generated somewhere else and I cannot configure them in any way so I cannot add any ngIf or anything else on that content, that would simplify my work... I just have to use them. What I want to achieve is something like this( jQuery version):
$("#button").click(function() {
$('.fc-oss').toggle();
});
I have a toggle and I want to attach an event to it that when it's being activated, it hides/ shows all elements with that specific type from the view. I am trying to achieve this in angular without jQuery but with little success. Any ideas on how I might be able to achieve this ?
I have a small issue that I can't figure out how to do. I am just learning Angular / Typescript and I can't figure out how I could remove some DOM elements. I have some auto generated content, that has specific CSS classes. Unfortunately The objects are generated somewhere else and I cannot configure them in any way so I cannot add any ngIf or anything else on that content, that would simplify my work... I just have to use them. What I want to achieve is something like this( jQuery version):
$("#button").click(function() {
$('.fc-oss').toggle();
});
I have a toggle and I want to attach an event to it that when it's being activated, it hides/ shows all elements with that specific type from the view. I am trying to achieve this in angular without jQuery but with little success. Any ideas on how I might be able to achieve this ?
Share Improve this question edited Jun 25, 2018 at 8:55 Vlad N asked Jun 25, 2018 at 8:39 Vlad NVlad N 6512 gold badges10 silver badges22 bronze badges3 Answers
Reset to default 4In Angular you can use direct access to DOCUMENT.
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
@Injectable()
export class MyService {
constructor(@Inject(DOCUMENT) private document: any) {}
}
Having this you can access your element and define some logic to it
let element = this.document.getElementbyClassName('fc-oss');
element.style.display = element.style.display === 'none' ? 'block' : 'none';
You can work with *ngIf="boleanVar"
, init it in your ts 'boleanVar = true'
in your ts and add <br>< button (click)="boleanVar = !boleanVar"> Toggle visibility</button>
You can use the ngif directive for that.
@Component({
selector: 'ng-if-simple',
template: `
<button (click)="show = !show">{{show ? 'hide' : 'show'}}</button>
show = {{show}}
<br>
<div *ngIf="show">Text to show</div>
`
})
class NgIfSimple {
show: boolean = true;
}
According to the value of the variable show
the div element will toggle view.
本文标签: javascriptAngular 5 remove specific DOM elementsStack Overflow
版权声明:本文标题:javascript - Angular 5 remove specific DOM elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740100785a2224453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论