Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript无法推送到knockoutObservableArray,因为即使在初始化之后也未定义_Javascript_Asp.net Mvc_Typescript_Knockout.js_Undefined - Fatal编程技术网

Javascript Typescript无法推送到knockoutObservableArray,因为即使在初始化之后也未定义

Javascript Typescript无法推送到knockoutObservableArray,因为即使在初始化之后也未定义,javascript,asp.net-mvc,typescript,knockout.js,undefined,Javascript,Asp.net Mvc,Typescript,Knockout.js,Undefined,我正在使用asp.NETMVC5和typescript。在我的viewmodel中,我试图将ajax调用中接收的数据从api推送到knockoutObservable数组。即使我的数组是initilizeor,至少我认为它应该是 错误:TypeError:无法读取未定义的属性“push” 这是我的密码: module Models { export class ReservationDay { date: KnockoutObservable<Date>; pla

我正在使用asp.NETMVC5和typescript。在我的viewmodel中,我试图将ajax调用中接收的数据从api推送到knockoutObservable数组。即使我的数组是initilizeor,至少我认为它应该是

错误:TypeError:无法读取未定义的属性“push”

这是我的密码:

module Models {

export class ReservationDay {

    date: KnockoutObservable<Date>;
    place: KnockoutObservable<string>;
    avaibleSlots: KnockoutObservable<number>;
    instructorId: KnockoutObservable<string>;

    constructor(date: Date, place: string, avaibleSlots: number, instructorId: string) {
        this.date = ko.observable(date);
        this.place = ko.observable(place);
        this.avaibleSlots = ko.observable(avaibleSlots);
        this.instructorId = ko.observable(instructorId);
    }

  }
}


module ViewModel {
    import ReservationDay = Models.ReservationDay;

export class Calendar {


    public days: KnockoutObservableArray<ReservationDay> = ko.observableArray<ReservationDay>();

    constructor() {
        this.getMonthCalendar(new Date());
    }


getMonthCalendar(date: Date) {
        var month = date.getMonth() + 1;
        $.ajax({
            url: 'myApiUrl' + month,
            type: 'GET',
            dataType: 'json',
            async: false,
            success(data, textStatus, xhr) {
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        console.log(this.days); // here is undefined
                        this.days.push(new ReservationDay(data[i].date,data[i].place,data[i].avaibleSlots, data[i].instructorId)); // in this line : error  : TypeError: Cannot read property 'push' of undefined
                    }
                    console.log("ajax done.");

                }
            },
            error(xhr, textStatus, errorThrown) {
                console.log('Error in Operation');
            }
        });
    }
以下是我的观点:

@section Scripts{
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/knockout")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/calendar")




<script type="text/javascript">
    $(function () {
        var vm = new ViewModel.Calendar(@Model);
        ko.applyBindings(vm);
    });
</script>

}
还有一个问题,有人能告诉我如何使用ReservationDay.ts类吗?该类位于其他文件夹中,不在具有上述viewmodel的文件中

提前谢谢你

因为ajax success中的这一点并不是指日历的实例,而是指ajax设置对象

您可以通过在ajax外部添加对日历实例的引用来解决此问题:

getMonthCalendar(date: Date) {
    var self = this;

    $.ajax({
      ........
      ........
      success: (data, textStatus, xhr) => {
        if (data.length > 0) {
         for (var i = 0; i < data.length; i++) {
            self.days.push((new ReservationDay(data[i].date,data[i].place,data[i].avaibleSlots, data[i].instructorId));
          }
        }
      }
    })
}
要在结构中导入ReservationDay类,可以执行以下操作:

import {ReservationDay} from "../viewmodel/calendar"

谢谢你,成功了!你知道我第二个问题的答案吗?如何将模型放在单独的文件中?
import {ReservationDay} from "../viewmodel/calendar"