亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

innerHTML - 在(點擊)事件中傳遞值

innerHTML - 在(點擊)事件中傳遞值

鴻蒙傳說 2023-07-06 16:59:25
如何向innerHTML中已設置的點擊事件傳入參數?成分html = "Lorem ipsum dolor sit amet, <mark (click)='hello('my name is what')'>consectetur adipiscing elit</mark>"constructor(private changeDetectorRef: ChangeDetectorRef) {}ngAfterContentChecked() {  this.changeDetectorRef.detectChanges();   if (this.showcaseContentText.nativeElement.querySelector('mark')) {     this.showcaseContentText.nativeElement      .querySelector('mark')      .addEventListener('click', this.hello.bind(this));   }}hello(test: string) {  console.log(test);}模板<div class="text-md-left text-muted mb-lg-6" [innerHTML]="html" style="font-size: 15px"></div>
查看完整描述

1 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

我可以使用 RxJS而不是hook來捕獲標簽click中的事件。我還使用 Angular 清理了 HTML 。markfromEventaddEventListenerAfterViewInitAfterContentCheckedDomSanitizer


嘗試以下操作


應用程序組件.ts


import { fromEvent, Subject } from "rxjs";

import { takeUntil } from "rxjs/operators";


...

export class AppComponent implements AfterViewInit, OnDestroy {

? @ViewChild("showcaseContentText") showcaseContentText: ElementRef<any>;

? html =

? ? "Lorem ipsum dolor sit amet, <mark (click)='hello('my name is what')'>consectetur adipiscing elit</mark>";

? closed$ = new Subject<any>();


? constructor(private cdr: ChangeDetectorRef) {}


? ngAfterViewInit() {

? ? this.cdr.detectChanges();

? ? fromEvent(

? ? ? this.showcaseContentText.nativeElement.querySelector("mark"),

? ? ? "click"

? ? )

? ? ? .pipe(takeUntil(this.closed$))

? ? ? .subscribe(e => console.log(e));

? }


? ngOnDestroy() {

? ? this.closed$.next();

? }

}

應用程序組件.html


<div #showcaseContentText class="text-md-left text-muted mb-lg-6" [innerHTML]="html | safe" style="font-size: 15px">

</div>

安全管道.ts


import { Pipe, PipeTransform } from "@angular/core";

import { DomSanitizer, SafeHtml } from "@angular/platform-browser";


@Pipe({

? name: "safe",

? pure: true

})

export class SafePipe implements PipeTransform {

? constructor(protected sanitizer: DomSanitizer) {}


? public transform(value: any): SafeHtml {

? ? return this.sanitizer.bypassSecurityTrustHtml(value);

? }

}

更新:<mark>單個標簽中有多個標簽innerHTML

在這種情況下,您可以使用querySelectorAll()該函數來代替querySelector()。此外,由于會有多個元素,您可以使用Array#mapwithfromEvent和 RxJSmap運算符來獲取各自的ids。

請注意,我們創建了多個訂閱流。因此標簽的數量越多mark,流的數量就越多。當組件關閉時(例如takeUntil使用),您需要關閉它。有更好的方法來處理多個訂閱(例如使用combineLatest),但它們都有自己的優點和缺點。我將把它們留給你來解決。

控制器

export class AppComponent implements AfterViewInit, OnDestroy {

? @ViewChild("showcaseContentText") showcaseContentText: ElementRef<any>;

? html =

? ? "Lorem ipsum dolor sit amet, <mark id='mark1' (click)='hello('my name is what')'>consectetur adipiscing elit</mark>. Lorem ipsum <mark id='mark2' (click)='hello('my name is two')'>dolor sit amet</mark>, consectetur adipiscing elit";

? closed$ = new Subject<any>();


? constructor(private cdr: ChangeDetectorRef) {}


? ngAfterViewInit() {

? ? this.cdr.detectChanges();

? ? const obs$ = Array.from(

? ? ? this.showcaseContentText.nativeElement.querySelectorAll("mark")

? ? ).map((el: HTMLElement) => fromEvent(el, "click").pipe(map(_ => el["id"])));


? ? obs$.forEach(obs =>

? ? ? obs.pipe(takeUntil(this.closed$)).subscribe({

? ? ? ? next: id => {

? ? ? ? ? console.log(id);

? ? ? ? }

? ? ? })

? ? );

? }


? ngOnDestroy() {

? ? this.closed$.next();

? }

}


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 243 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號