【问题标题】:How to update tab component after submit提交后如何更新选项卡组件
【发布时间】:2019-06-25 13:02:46
【问题描述】:

`我有一个使用 Bootstrap JS 选项卡的 Angular 6 应用程序。我的一个标签包含一个笔记列表。用户通过模态弹出窗口添加注释,列表会用新注释刷新。这很好用。但是,在选项卡的标题中,我有一个锚选项卡,反映了输入的注释数量。我的问题是,添加新注释时如何更新该数字?

应用程序是这样安排的:有一个user-details.component.html 显示所有选项卡。注释标签包含在 user-notes.component.htmluser-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-bootstrapng-bootstrap.github.io
  • @Dimanoid,我不知道你为什么说它“不合时宜”?
  • 这就是我理解你的问题的方式......因为Bootstrap JS Tabadds a note through a modal popup, and the list is refreshed with the new note。除了模板之外,我在您的代码中仍然看不到_notes。请显示您更新它的代码。

标签: javascript angular angular-ui-bootstrap


【解决方案1】:

创建一个DataService,它将拥有您的privatelistOfItems,一个privateBehaviorSubject,可用于通知其他组件有关list的更改,同样,公开为public Observable.

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

@Injectable()
export class DataService {

  private listOfItems: Array<string> = [];
  private list: BehaviorSubject<Array<string>> = new BehaviorSubject<Array<string>>(this.listOfItems);
  public list$: Observable<Array<string>> = this.list.asObservable();

  constructor() { }

  addItemToTheList(newItem: string) {
    this.listOfItems.push(newItem);
    this.list.next(this.listOfItems);
  }

}

在所有三个组件中注入此服务,HeaderAddList。并相应地使用它。

这里有一个Working Sample StackBlitz 供您参考。

【讨论】:

  • 非常感谢您的详尽回答。我必须进行一些修改以适应我的代码,但我现在明白 Observables 可以工作了!再次感谢!
【解决方案2】:

在这里,您尝试在不同的角度组件之间进行通信。 为此,您可以使用服务或侦听添加注释的组件发出的事件。

您可以在这里找到更多信息:component-interaction

【讨论】:

  • 谢谢!有什么方法可以帮助我将组件交互应用到我的用例中?我的 UI 技能有限...
  • @user1660256 能否请您简要介绍一下您的组件是如何排列的,包含 HTML sn-p 的文件的名称是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多