Javascript并使用MVC风格的模型(带有角度)

Javascript and using an MVC style model (with angular)

本文关键字:模型 MVC 风格 Javascript      更新时间:2023-09-26

我有一个对象想用作角度控制器的模型。

var SchedulingModel = function() {
    this.weekdays = Weekdays;
    this.scheduleType = "2";
    this.customerType = "1";
    this.timesOfTheMonth = TimesOfTheMonth;
    this.waivers = [];
}

角度控制器:

$scope.model = SchedulingModel;

当我尝试访问 model.weekdays 和 model.timesOfTheMonth 时,它们都是未定义的......

console.log($scope.model.timesOfTheMonth);
console.log($scope.model.weekdays);

这是为什么呢?

你需要做新的调度模型

$scope.model = new SchedulingModel();

在这种情况下,它不是有棱角的东西。请尝试:

console.log(SchedulingModel.weekdays) //undefined
// but this will work
var TimesOfTheMonth = '';
var Weekdays = "";
var a = new SchedulingModel();
console.log(a.weekdays); //works ok