好的,所以如果我用淘汰赛做这样的事情就行得很好
function UserModel()
{
UserID = ko.observable();
Name = ko.observable();
}
function UserSettingsViewModel()
{
var user = UserModel();
this.Save = function(user) { SaveUser(user);};
}
ko.applyBindings(new UserSettingsViewModel());
尝试用打字稿做同样的事情,但它并没有真正起作用
declare var ko: any;
interface IUser {
UserID: any;
Username: any;
Name: any;
LastName: any;
CreatedOn: any;
LastLogin: any;
Picture: any;
}
class User implements IUser{
UserID = ko.observable('');
Username = ko.observable('');
Name = ko.observable('');
LastName = ko.observable('');
CreatedOn= ko.observable('');
LastLogin= ko.observable('');
Picture = ko.observable('');
}
function SaveUser(userSetting: any) : void
{
alert("I was saved!");
}
function EditUser(userSetting: any): void {
alert("I was Edited!");
}
function DeleteUser(userSetting: any): void {
alert("I was Deleted!");
}
class UserSettingsViewModel
{
userSetting: any;
constructor()
{
this.userSetting = (<any>User)();
}
Save()
{
SaveUser(this.userSetting);
}
Delete()
{
DeleteUser(this.userSetting);
}
Edit()
{
EditUser(this.userSetting);
}
}
window.onload = () => {
var ViewModel = new UserSettingsViewModel();
ko.applyBindings(ViewModel);
};
我如何使用TypeScript做同样的事情,我尝试了不同的方法,但它们没有用。
答案 0 :(得分:0)
偷偷摸摸的怀疑。您可能错了this
。使用lambda:
function SaveUser(userSetting: any) : void
{
alert("I was saved!");
}
function EditUser(userSetting: any): void {
alert("I was Edited!");
}
function DeleteUser(userSetting: any): void {
alert("I was Deleted!");
}
class UserSettingsViewModel
{
userSetting: any;
constructor()
{
this.userSetting = (<any>User)();
}
Save = () =>
{
SaveUser(this.userSetting);
}
Delete = () =>
{
DeleteUser(this.userSetting);
}
Edit = () =>
{
EditUser(this.userSetting);
}
}
有关this
的更多信息:https://www.youtube.com/watch?v=tvocUcbCupA
答案 1 :(得分:0)
显然我的代码是正确且有效的,错误的是我的HTML文档,我有$ root.Name,这就是为什么它不起作用,它现在正常工作。