【问题标题】:Data only displayed after refresh数据只在刷新后显示
【发布时间】:2018-12-04 12:51:23
【问题描述】:

我正在我的应用程序中创建 cmets 部分,当用户输入 cmets 并提交时,评论应该立即显示在前端,不幸的是现在 cmets 仅在刷新后可见,

这是我所拥有的:

显示 cmets

组件.ts

  ngOnInit() {
       this.activeRouter.params.subscribe((params) => {
      // tslint:disable-next-line:prefer-const
      let id = params['id'];
      this.moviesService.getComments(id)
        .subscribe(comments => {
          console.log(comments);
          this.comments = comments;
        });
    });
}

service.ts

 getComments(id: string): Observable<any> {
    const url = `${apiUrl + this.commentsUrl}/${id}`;
    return this.http.get(url, httpOptions).pipe(
      map(this.extractData),
      catchError(this.handleError));
  }

HTML

<div *ngFor="let comment of comments" class="col-md-7">
          <ul class="list-group">
            <li class="list-group-item">Author: {{comment.author}}</li>
            <li class="list-group-item">Comments: {{comment.description}}</li>
          </ul>
          <br>
        </div>

**

添加 cmets:

** 服务.ts

  // Adds comments
      addReview(author, description) {
        const uri = 'http://localhost:8000/movies/comments';
        const obj = {
          author: author,
          description: description
        };
        return this.http.post(uri, obj);
      }

compo.ts

  createForm() {
    this.angForm = this.fb.group({
      author: ['', Validators.required],
      description: ['', Validators.required]
    });
  }
  addReview(author, description) {
    this.moviesService.addReview(author, description).subscribe(success => {
      this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
    }, error => {
      this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
    });
  }

HTML

<div class="col-md-7 movie-form" >
          <flash-messages></flash-messages>
          <form [formGroup]="angForm" novalidate>
            <div class="form-group">
              <label class="col-md-4">Author</label>
              <input type="text" class="form-control" name="author" formControlName="author" #author />
            </div>
            <div *ngIf="angForm.controls['author'].invalid && (angForm.controls['author'].dirty || angForm.controls['author'].touched)"
              class="alert alert-danger">
              <div *ngIf="angForm.controls['author'].errors.required">
                Name is required.
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-4">Description</label>
              <textarea cols="10" rows="10" type="text" class="form-control" formControlName="description" #description>
                </textarea>
            </div>
            <div *ngIf="angForm.controls['description'].invalid && (angForm.controls['description'].dirty || angForm.controls['description'].touched)"
              class="alert alert-danger">
              <div *ngIf="angForm.controls['description'].errors.required">
                description is required.
              </div>
            </div>
            <div class="form-group">
              <button (click)="addReview(author.value, description.value)" [disabled]="angForm.pristine || angForm.invalid" class="btn btn-primary">Add</button>
            </div>
          </form>
        </div>

问题

我的代码有什么问题?任何帮助建议将不胜感激

【问题讨论】:

  • 我认为你应该在添加新的更新 cmets 列表后调用 getComments
  • 你实际上并没有展示你是如何创建新评论的——这会触发新的抓取吗?它不会因为你知道现在有新数据而神奇地发出另一个 GET 请求; Angular 不知道。
  • 什么意思?你可以说得更详细点吗?我对所有这些东西都很陌生
  • 请提供创建评论的方法
  • @KasabuckiAlexandr 更新了 qn

标签: angular html typescript


【解决方案1】:

@Kaczkapojebana。当我们订阅“get”时,并不意味着视图中显示了 dbs 中的更改。 (只说当异步调用完成时,显示数据)。我通常将“get”订阅称为“仅使用一次订阅”。

您必须手动将新数据添加到 this.cmets。在哪里? IN订阅功能:

addReview(author, description) { 
    this.moviesService.addReview(author, description).subscribe(success => {  
      /***add manually to this.comments***********/
      this.comments.push({author:author,descripcion:description});

      this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
    }, error => {
      this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
    });
  }

注意:您也可以再次订阅以获取所有数据或 addReview 响应所有数据 注意2:同样,当你进行更新时,你必须手动更新“this.cmets”数组

【讨论】:

    【解决方案2】:

    您必须在提交 cmets 的回调中调用您的 getComments 方法。您的代码在组件的ngOnInit 方法中只有一个“get”调用。因此,当您刷新视图时,ngOnInit 会再次执行,因此会调用 getComments

    您必须在提交 cmets 方法的回调中进行 get 调用。

    编辑#1:

    addReview(author, description) {
        this.moviesService.addReview(author, description).subscribe(success => {
            this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
            // get the id
            this.moviesService.getComments(id).subscribe(comments => {
                console.log(comments);
                this.comments = comments;
            });
        }, error => {
            this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
        });
    }
    

    【讨论】:

    • 您必须在提交 cmets 方法的回调中进行 get 调用。你到底是什么意思?正在学习,你能给我举个例子吗?
    • 你能解释一下你的意思吗?您必须在提交 cmets 方法的回调中进行 get 调用。
    猜你喜欢
    • 2018-05-13
    • 2021-10-30
    • 2020-02-10
    • 1970-01-01
    • 2018-06-19
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    相关资源
    最近更新 更多