eval上的Angular 2 undefined var

时间:2017-07-26 19:19:21

标签: javascript angular ionic-framework ionic2

所以我有以下代码:

@ViewChild('MondayPicker') MondayPicker;
@ViewChild('TuesdayPicker') TuesdayPicker;
@ViewChild('WednesdayPicker') WednesdayPicker;
@ViewChild('ThursdayPicker') ThursdayPicker;
@ViewChild('FridayPicker') FridayPicker;
@ViewChild('SaturdayPicker') SaturdayPicker;
@ViewChild('SundayPicker') SundayPicker;

working_days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
simpleColumns;
reservation_times;
currentUser;
private ngUnsubscribe: Subject<any> = new Subject<any>()

ionViewDidEnter(){

    for(var i = 0; i < this.working_days.length; i++){
        this.firebase.database.list('/schedule',{query: {orderByChild: 'user_day',equalTo: this.currentUser.uid+'_'+this.working_days[i] }})
        .take(1)
        .subscribe(
        (data) => {
            console.log(eval('this.'+data[0].day+'Picker.setValue("From 00: 00 TO 00: 00");'));
        });
    }
}

现在这一行:

  

console.log(eval('this。'+ data [0] .day +'Picker.setValue(“从00:00到   00:00“);'));

所以我要做的就是产生这个:this.MondayPicker.setValue("From 00: 00 TO 00: 00");

但我在undefined上获得了this.MondayPicker,但如果我手动this.MondayPicker.setValue("From 00: 00 TO 00: 00");它可行,那么为什么会这样?

1 个答案:

答案 0 :(得分:0)

我认为你应该避免使用eval。试试这个:

this[data[0].day + 'Picker'].setValue(...)