Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
現在是一個 Angular專家,請幫我用 Angular寫一個可以給其他多個Component調用的Component,它需要實現留言版的功能,並且使用Quill…
- User
現在是一個 Angular專家,請幫我用 Angular寫一個可以給其他多個Component調用的Component,它需要實現留言版的功能,並且使用Quill Mention套件實現一個可以mention somebody的文字編輯器和附檔上傳功能,盡量使用primng 元件
- Assistant
好的,讓我們開始編寫這個留言版的Angular組件。
首先,我們需要創建一個Angular組件,讓其他多個Component可以調用。假設我們的留言版組件名為
CommentBoxComponent。- 創建
CommentBoxComponent組件
rustng generate component comment-box- 在
comment-box.component.html文件中,添加Quill Editor並使用Quill Mention套件。
css<p-editor [(ngModel)]="commentContent" [style]="{'height':'200px'}" [modules]="quillModules"></p-editor> <ng-template #mentionList let-list> <div *ngFor="let item of list"> <p>{{item.label}}</p> </div> </ng-template>在這裡,我們使用了PrimeNG中的
p-editor元件來實現文字編輯器。為了實現提到某人的功能,我們還需要添加一個ng-template,這個ng-template會被Quill Mention套件使用。在ng-template中,我們使用了ngFor指令來循環遍歷提到的人的列表。接下來,我們需要在組件中定義一些屬性和方法。
- 在
comment-box.component.ts文件中,添加屬性和方法。
typescriptimport { Component, OnInit } from '@angular/core'; import { MentionModule } from 'primeng/mention'; import Quill from 'quill'; @Component({ selector: 'app-comment-box', templateUrl: './comment-box.component.html', styleUrls: ['./comment-box.component.scss'] }) export class CommentBoxComponent implements OnInit { commentContent: string; quillModules: any; constructor() { this.quillModules = { mention: { allowedChars: /^[A-Za-z\sÅÄÖåäö]*$/, source: (searchTerm, renderList) => { let matches = []; if (searchTerm.length === 0) { renderList(this.mentionList, searchTerm); } else { const users = this.mentionList.filter(item => item.label.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1); users.forEach(user => { matches.push({ value: user.label, userId: user.id }); }); renderList(matches, searchTerm); } }, }, }; } mentionList = [ { id: 1, label: 'Alice' }, { id: 2, label: 'Bob' }, { id: 3, label: 'Charlie' }, { id: 4, label: 'David' }, { id: 5, label: 'Emma' } ]; ngOnInit(): void { } handleFileInput(files: FileList) { // handle file upload } handleSubmit() { // handle comment submission } } - 創建
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.