Angularjs 在两个角度组件之间同步数据
我正在尝试在两个角度分量之间同步数据。然而,它的功能有点不可预测 我有提供数据(同步)的父组件:Angularjs 在两个角度组件之间同步数据,angularjs,typescript,angular,Angularjs,Typescript,Angular,我正在尝试在两个角度分量之间同步数据。然而,它的功能有点不可预测 我有提供数据(同步)的父组件: @组件({ /*...*/ 指令:[TBInfiniteSrollComponent], viewBindings:[TBInfiniteSrollComponent], 提供者:[TbService], 封装:视图封装。无 }) 导出类TbNiV3AppComponent{ 公共dealerList:Array=[] 构造函数(服务:TbService,专用cdr:ChangeDetectorRe
@组件({
/*...*/
指令:[TBInfiniteSrollComponent],
viewBindings:[TBInfiniteSrollComponent],
提供者:[TbService],
封装:视图封装。无
})
导出类TbNiV3AppComponent{
公共dealerList:Array=[]
构造函数(服务:TbService,专用cdr:ChangeDetectorRef){
this.dealList=service.data.flatte(service.data.get());
}
同步数据(数据){
/*不幸的是,这不起作用*/
console.log('syncData!',this.dealerList);
this.dealList=[].concat(数据);
this.cdr.markForCheck();
}
}
和它的HTML
<tb-infinite-scroll [tbScrollData]="dealerList" *ngIf="dealerList" (tbDataSync)="syncData($event)" [tbIndent]="true">
Loading Tree Component...
</tb-infinite-scroll>
<tb-infinite-scroll [tbScrollData]="dealerList" *ngIf="dealerList" (tbDataSync)="syncData($event)">
Loading Tree Component...
</tb-infinite-scroll>
正在加载树组件。。。
正在加载树组件。。。
然后我有了子组件
@Component({
/* ... */
selector: 'tb-infinite-scroll',
inputs: ['rows:tbScrollData', 'tbElHeight:elementHeight', 'tbIndent'],
outputs: ['tbDataSync'],
encapsulation: ViewEncapsulation.None,
providers: [TbInfiniteScrollService, ChangeDetectorRef],
})
export class TbInfiniteScrollComponent {
public tbDataSync:EventEmitter<any> = new EventEmitter()
private rows:Array<Object> = []
/* ... */
assignRows() {
this.visibleRows = this.service.assignVisible(this.rows, this.elementHeight, this.scroll);
}
constructor(private service:TbInfiniteScrollService, private elementRef:ElementRef, private cdr:ChangeDetectorRef) {
}
toggleVisible(children) {
this.service.changeVisibility(children);
this.assignRows();
this.tbDataSync.emit(this.rows);
}
}
@组件({
/* ... */
选择器:“tb无限滚动”,
输入:['rows:tbScrollData','tbElHeight:elementHeight','tbIndent'],
输出:['tbDataSync'],
封装:视图封装。无,
提供者:[TbInfiniteScrollService,ChangeDetectorRef],
})
导出类TbInfiniteScrollComponent{
公共tbDataSync:EventEmitter
当绑定数据更改时,如何强制组件从父组件刷新?
提前谢谢
编辑:通过引用链接数据对象
@Component({
/* ... */
selector: 'tb-infinite-scroll',
inputs: ['rows:tbScrollData', 'tbElHeight:elementHeight', 'tbIndent'],
outputs: ['tbDataSync'],
encapsulation: ViewEncapsulation.None,
providers: [TbInfiniteScrollService, ChangeDetectorRef],
})
export class TbInfiniteScrollComponent {
public tbDataSync:EventEmitter<any> = new EventEmitter()
private rows:Array<Object> = []
/* ... */
assignRows() {
this.visibleRows = this.service.assignVisible(this.rows, this.elementHeight, this.scroll);
}
constructor(private service:TbInfiniteScrollService, private elementRef:ElementRef, private cdr:ChangeDetectorRef) {
}
toggleVisible(children) {
this.service.changeVisibility(children);
this.assignRows();
this.tbDataSync.emit(this.rows);
}
}
<div class="tb-infinite-scroll__row"
*ngFor="let row of visibleRows"
[ngStyle]="{'margin-left': (tbIndent ? (row.level - 1) * 15 : 0) + 'px'}"
(click)="row.children && toggleVisible(row.children)">
{{row.name}}
</div>