简体   繁体   English

Angular HTTP 调用不会触发? 没发生什么事

[英]Angular HTTP call does not triggers? Nothing happens

I'm new to Angular of course :) I have a list and I use [(ngModel)] to display an element of this list.我当然是 Angular 的新手 :) 我有一个列表,我使用 [(ngModel)] 来显示这个列表的一个元素。 Then I want to update this selected element and persist the changes with a button.然后我想更新这个选定的元素并用一个按钮保持更改。 this is my code :这是我的代码:

html component html组件

<div *ngIf="selectedAction">
  <label>Titre :
    <input [(ngModel)]="selectedAction.title" />
  </label><br />
  <label>Détails :
    <input [(ngModel)]="selectedAction.details" />
  </label><br />
  <label>Date limite :
      <input [ngModel]="selectedAction.date_limite | date:'dd/MM/yyyy'" (ngModelChange)="selectedAction.date_limite=$event" />
  </label><br />
  <button type="button" class="btn btn-primary"
    (click)="save();"
    style="float: none;">Modifier</button>
  <button type="button"class="btn btn-primary"
    (click)="clear()"
    style="float: none;">Cacher</button>
</div>
<!-- Affichage de la liste des actions -->
<h2>Ma liste d'action</h2>
  <ul class="list-group">
    <!-- Lors du click sur l'élément de la liste, la méthode onSelect() s'exécute -->
    <li *ngFor="let action of actions" (click)="onSelect(action)" class="list-group-item" style="padding: 0.25rem 1.25rem;">
      <span>{{ action.title | uppercase }}</span>
      <span>: {{ action.details }}</span>
        <button type="button" class="btn btn-primary" title="delete action"
          (click)="delete(action)">Archiver</button>
    </li>
  </ul>

ts component with the save() action for the 'Modifier' button带有“修改器”按钮的 save() 操作的 ts 组件

save(): void {
    this.actionService.updateAction(this.selectedAction);
  }

Then the service component然后是服务组件

updateAction (action: Action): Observable<any> {
    return this.http.put(this.actionsUrl, action, httpOptions).pipe(
      catchError(this.handleError<any>('updateAction'))
    );
  }

Actually, when I click on the button, nothing happens.实际上,当我单击按钮时,什么也没有发生。

Thx for helping.感谢帮助。

Whenever you make a call to HTTP in Angular (which is based on RxJS Observable), you should subscribe it.每当您在 Angular(基于 RxJS Observable)中调用 HTTP 时,您都应该订阅它。 You forgot to subscribe the updateAction() method which returns an Observable.您忘记订阅返回 Observable 的 updateAction() 方法。 Only when you subscribe to an Observable , the HTTP call will happen.只有当您订阅Observable 时,才会发生 HTTP 调用。

save(): void {
    this.actionService.updateAction(this.selectedAction).subscribe();
 }

You need to update the list in the subscribe method.您需要更新订阅方法中的列表。 Which will in return re render in html to produce the output.这将作为回报重新呈现在 html 中以产生输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM