我的设计页面上有两个分页器。当我添加两个分页器时-他们的数据不同步。 https://material.angular.io/components/paginator
https://plnkr.co/edit/w5ZuXnfIgmKW1nWgyf3Y?p=preview
<mat-paginator [length]="length"
[pageSize]="10">
</mat-paginator>
<mat-paginator [length]="length"
[pageSize]="10">
</mat-paginator>
Run Code Online (Sandbox Code Playgroud)
如何做到这一点?
如何从单元测试中访问注入的ngControl?如何解决错误?
在组件中
constructor(
@Self() @Optional() public ngControl: NgControl
) { }
ngOnInit(): void {
this.ngControl.valueChanges
Run Code Online (Sandbox Code Playgroud)
在单元测试中
beforeEach(() => {
fixture = TestBed.createComponent(component);
fixture.detectChanges();
Run Code Online (Sandbox Code Playgroud)
执行测试时得到错误
TypeError: Cannot read property 'valueChanges' of null
我通过在每次点击时传递一个唯一的日期来解决这个问题.但没有约会,有没有一个好方法呢?
@Component({
selector: 'child'
})
export class ChildComponent {
@Input()
public set onClick(value: Date) {
if (value) {
doSomething();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在父模板中:
<child [onClick]="timestamp"></child>
<button (click)="timestamp= new Date"></button>
Run Code Online (Sandbox Code Playgroud) 你可能都知道你需要取消订阅Observables以防止内存泄漏。
我不明白为什么不只为使用 Observables 的组件类创建@Uncsubscribe装饰器,它将遍历所有属性并取消订阅destroy上的所有订阅。
或者更多的糖:扩展@Component装饰器来添加这样的行为。
现在的问题是:这个解决方案可能存在哪些缺陷?