我有一个父组件(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变量做一个.处理这个问题的最佳方法是什么?
我有一个名为的组件,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)