我有一個管道,它回傳一個包含標簽的元素陣列
arr=''
spl!:string;
transform(value: any, ...args: any[]) {
this.arr=''
this.spl = value.split(' ')
for (const iterator of this.spl) {
if(iterator.charAt(0)==='#'){
this.arr=value
console.log(iterator);
}
}
return this.arr;
}
在我的 ts 檔案中,我在函式中添加了一個文本
this.noteService.getNoteById(this.queryParam).subscribe((res)=>{
this.noteModel = res
this.noteData = res.note
this.textValue=Object(this.noteData).text
this.textValue= this.format.transform(this.textValue) //this is the pipe
this.textForm.controls['text'].setValue(this.textValue)// here I'm writing into textarea
})
函式回傳所有標簽,如何在 span 標簽中添加標簽并在 textarea 中寫入文本?
uj5u.com熱心網友回復:
要在 textarea 中顯示文本,您可以textValue像這樣將變數系結到 textarea(更多資訊在這里):
<textarea [(ngModel)]="textValue"></textarea>
對于單獨顯示標簽(更多關于模板中的管道使用和迭代):
<div class="tags">
<span *ngFor="let tag of textValue | tags">{{ tag }}</span>
</div>
你可以這樣寫一個管道:
@Pipe({
name: 'tags'
}) class TagsPipe {
transform(value: any, ...args: any[]) {
return value?
.split(' ')
.filter(w => w.startsWith('#');
}
}
在您的組件中:
this.noteService.getNoteById(this.queryParam).subscribe((res) => {
this.noteModel = res;
this.noteData = res.note;
this.textValue = (this.noteData as any).text;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/421059.html
標籤:
