私は以下のTypeScriptクラスを持っています。Typeクラス内のjquery関数スコープ内のメソッドを呼び出す
export class BrandViewModel {
private _items = ko.observableArray();
public Add(id: number, name: string, active: boolean) : void {
this._items.push(new BrandItem(this, id, name, active));
}
public Get() : void {
$.get("/api/brand", function(items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
}
Get
メソッドの結果としてのjavascriptは次のとおりです。私はこれを行うことができますTypeScript
ドキュメントで見てきた
BrandViewModel.prototype.Get = function() {
$.get("/api/brand", function (items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
};
:以下、どこになり
public Get() : void {
$.get("/api/brand",() => function(items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
_this
はBrandViewModel
インスタンスへの参照になりましたが、this
はjquery内にあります機能は、私が期待するかもしれないと_this
に変更されていません。
BrandViewModel.prototype.Get = function() {
var _this = this;
$.get("/api/brand", function() {
return function (items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
};
}, "json");
};
は、代わりに私は以下の行っているTypeScript
に:
BrandViewModel.prototype.Get = function() {
var _this = this;
$.get("/api/brand", function (items) {
$.each(items, function (i, item) {
_this.Add(item.Id, item.Name, item.Active);
});
}, "json");
};
:私が望んでいた結果を与える
public Get(): void {
var _this = this;
$.get("/api/brand", function(items) {
$.each(items, function (i, item) {
_this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
を誰かがこれを行うより適切な方法を知っていますか?
public Get() : void {
$.get("/api/brand", (items) => {
$.each(items, (i, item) => {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
生成する:
私のデモについて教えてください。なぜそれが実行できないのか分かりません。https://stackoverflow.com/questions/46834032/uncaught-typeerror-this-delete-is-not-a-function – ziqi