मेरे पास नीचे टाइपस्क्रिप्ट क्लास है।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
विधि के लिए जिसके परिणामस्वरूप जावास्क्रिप्ट है:
BrandViewModel.prototype.Get = function() {
$.get("/api/brand", function (items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
};
मैं TypeScript
प्रलेखन कि मैं यह कर सकता में देखा है:
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के अंदर है TypeScript
में
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");
};
इसके बजाय मैं नीचे किया है::समारोह के रूप में मैं उम्मीद कर सकते हैं _this
करने के लिए बदल नहीं है
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");
}
जो मुझे परिणाम मैं चाहता था देता है:
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");
};
क्या कोई इसे करने के लिए एक और अधिक उचित तरीका जानता है?
तुम मुझे मेरे demo.I के बारे में मदद नहीं जानता कि क्यों यह https://stackoverflow.com/questions/46834032/uncaught-typeerror-this-delete-is-not-a-function नहीं चलाए जा सकते हैं – ziqi