【发布时间】:2019-06-25 13:02:46
【问题描述】:
`我有一个使用 Bootstrap JS 选项卡的 Angular 6 应用程序。我的一个标签包含一个笔记列表。用户通过模态弹出窗口添加注释,列表会用新注释刷新。这很好用。但是,在选项卡的标题中,我有一个锚选项卡,反映了输入的注释数量。我的问题是,添加新注释时如何更新该数字?
应用程序是这样安排的:有一个user-details.component.html 显示所有选项卡。注释标签包含在 user-notes.component.html 和 user-notes.component.ts 中(发布在下面)。
例如,这里是user-detail.component.html中一些标签的html:
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
<li class="active"><a href="#entitlements" data-toggle="tab" [class.disabled]="isEntitlementTabDisabled">Entitlements</a></li>
<li><a href="#payment_instruments" data-toggle="tab" style="display: none">Payment Instruments</a></li>
<li><a href="#notes" data-toggle="tab" >Notes ({{_notes.length}})</a></li> <!--style="display: none" -->
</ul>
请注意,“Notes”链接引用了{{_notes.length}}。我需要在发布时更新_notes.length,但我完全不确定如何。有人可以帮忙吗?
编辑:这是我的组件代码:
import { AuthGuard } from '../../service/auth-guard.service';
import { Router } from '@angular/router';
import { Logger } from './../../service/logger.service';
import { Component, OnInit, Input } from '@angular/core';
import { UserDetailService } from '../../user/service/user-detail.service';
import { UserEntitlementService } from '../../user/service/user-entitlement.service';
import { Note } from '../../user/model/note.model';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-notes-component',
templateUrl: './user-notes.component.html'
})
export class UserNotesComponent implements OnInit {
@Input() asRegIdofUser;
@Input()
private notesModel: Note[]=[];
private actionResult: string;
private notesCount: number;
private currentNote: Note;
constructor(private _logger: Logger, private _userDetailService: UserDetailService,
private _router: Router, private _userEntitlementService: UserEntitlementService,
private authGuard: AuthGuard) {
}
ngOnInit(): void {
//read data....
this.currentNote= new Note();
if (this.asRegIdofUser)
this.refreshNotesData();
}
refreshNotesData(){
this.actionResult='';
this._userDetailService.getNotes(this.asRegIdofUser).subscribe(
responseData =>{
let embedded = JSON.parse(JSON.stringify(responseData));
let notes = embedded._embedded.note
this.notesModel=[];
notes.forEach(note => {
this.notesModel.push(note);
})
this.notesCount=this.notesModel.length;
},
error =>{
this._logger.error("error on loading notes "+error);
}
)
this.currentNote= new Note();
}
onCreateNote(notesModal){
this._userDetailService
.postNote(this.asRegIdofUser,this.currentNote).subscribe(
response => {
if (response==='OK')
this.actionResult='success';
else
this.actionResult='failure';
},error => {
this.actionResult='failure';
}
)
}
userHasEditRole(): boolean{
return this.authGuard.hasAccess('edituserdetails');
}
onDelete(noteId: string){
let deleteNoteId: number = Number.parseInt(noteId);
this._userDetailService.deleteNote(this.asRegIdofUser,deleteNoteId).
subscribe(
response =>{
if(response == 'OK')
this.refreshNotesData();
},
error =>{
this._logger.error("error on deleting notes "+error);
}
)
}
}
【问题讨论】:
-
请也分享组件js
-
据我了解
_notes是在角度之外更新的?因此,您需要标记您的数据以供 ChangeDetector 进行检查。 angular.io/api/core/ChangeDetectorRef#markforcheck 但我建议不要重新发明轮子并使用某种角度引导库,如 valor-software.com/ngx-bootstrap 或 ng-bootstrap.github.io -
@Dimanoid,我不知道你为什么说它“不合时宜”?
-
这就是我理解你的问题的方式......因为
Bootstrap JS Tab和adds a note through a modal popup, and the list is refreshed with the new note。除了模板之外,我在您的代码中仍然看不到_notes。请显示您更新它的代码。
标签: javascript angular angular-ui-bootstrap