我正在尝试使用该nb-stepper
组件,并想selectedIndex
在我的角度组件中设置。在刷新我的活动后指定双向绑定时[(selectedIndex)]
,nb-step 不会更新以显示所选步骤。
在我的组件中,我为 selectedActivityIndex 添加了一个 Input(),nb-stepper
如果我在ngOnInit()
. 但后来,在获得新的活动列表后,我似乎无法通知步进器新的selectedIndex
.
活动.html
<nb-stepper orientation="vertical" disableStepNavigation="true" [(selectedIndex)]="selectedActivityIndex">
<nb-step *ngFor="let activity of activities"
[label]="activity.name"
[completed]="activity.completed"
></nb-step>
</nb-stepper>
活动组件.ts
export class ActivitiesComponent {
private _selectedActivityIndex: number;
@Output() onSelectedActivityIndexChange = new EventEmitter();
@Input()
get selectedActivityIndex() {
return this._selectedActivityIndex;
}
set selectedActivityIndex(val) {
this._selectedActivityIndex = val;
this.onSelectedActivityIndexChange.emit(this.selectedActivityIndex);
}
constructor(private activitiesService: ActivitiesService) {
this.activities = []
}
ngOnInit() {
this.selectedActivityIndex = 0
this.activitiesService.getActivities()
.subscribe(data => {
if(data['statusCode'] == 200) {
this.activities = data['activities'];
this.selectedActivityIndex = this.activities.map(m => { return m.selected; }).indexOf(true, 0)
}
else
console.log('Failure to get activities: ' + data['statusCode'])
});
}
我希望将selectedIndex
其更新为服务返回的 JSON 响应中指定的值,实际上即使this.selectedActivityIndex = 1
在订阅中设置也不起作用。