【发布时间】:2018-11-26 16:37:06
【问题描述】:
js My Course 在更改时不订阅可观察数组。控制台日志不显示。我不知道如何解决它。有什么想法吗?
class Course {
constructor(data) {
this.id = ko.observable(data.id);
this.name = ko.observable(data.name);
this.lecturer = ko.observable(data.lecturer);
this.name.subscribe(function(newName) {
console.log(newName);
});
this.lecturer.subscribe(function(newLecturer) {
console.log(newLecturer);
});
}
}
function ProtoModel() {
var self = this;
self.courses = ko.observableArray([]);
self.addCourse = function() {
const newCourse = new Course({
name: this.newCourseNameText(),
lecturer: this.newCourseLecturerText()
});
self.courses.push(ko.mapping.toJS(newCourse));
self.newCourseNameText("");
self.newCourseLecturerText("");
};
}
var model = new ProtoModel();
ko.applyBindings(model);
【问题讨论】:
标签: knockout.js observable subscribe