相关疑难解决方法(0)

如何检测Angular中@Input()值何时发生变化?

我有一个父组件(CategoryComponent),一个子组件(videoListComponent)和一个ApiService.

我有大部分工作正常,即每个组件可以访问json api并通过observable获取其相关数据.

目前视频列表组件只是获取所有视频,我想将其过滤为特定类别中的视频,我通过将categoryId传递给子项来实现此目的@Input().

CategoryComponent.html

<video-list *ngIf="category" [categoryId]="category.id"></video-list>
Run Code Online (Sandbox Code Playgroud)

这有效,当父CategoryComponent类别更改时,categoryId值将通过via传递,@Input()但我需要在VideoListComponent中检测到这一点并通过APIService(使用新的categoryId)重新请求视频数组.

在AngularJS中,我会对$watch变量做一个.处理这个问题的最佳方法是什么?

angular2-changedetection angular

382
推荐指数
13
解决办法
23万
查看次数

执行 POST 操作后刷新组件

我有一个名为的组件,customers-list我在其中显示 API 中的所有客户:

客户列表.html

<div *ngFor="let customer of customers">
     <p>{{customer.name}</p>
</div>
Run Code Online (Sandbox Code Playgroud)

客户列表.ts

import { Component Input} from '@angular/core';
import { ICustomer} from 'src/app/models/app.models';
import { CustomersService } from 'src/app/services/customers.service';

@Component({
  selector: 'drt-customers-list',
  templateUrl: './customers-list.component.html',
  styleUrls: ['./customers-list.component.scss'],
})
export class CustomerListComponent {
 public customers:  ICustomer[] ;

 constructor(public customersService: CustomersService,) {}

  public async ngOnInit(): Promise<void> {
    this.customers = await this.customersService.getCustomersList('');
  }

}
Run Code Online (Sandbox Code Playgroud)

我有另一个名为 的组件add-customer,我将在其中添加新客户,如下所示:

public onaddCustomer(): void {
    this.someCustomer = this.addCustomerForm.value;
    this.customersService.addCustomer( this.someCustomer).subscribe(
      () => { // If …
Run Code Online (Sandbox Code Playgroud)

typescript angular-services angular angular6

5
推荐指数
1
解决办法
8839
查看次数