I am a beginner in typescript and angular.js, and I am facing difficulties with an http get request. I rely on DefinitelyTyped for angular's type definitions.
This is what my controller code looks like:
module game.Controller {
'use strict';
export interface IGameScope extends ng.IScope {
vm: GameCtrl;
}
export class GameCtrl {
private bonus: any;
private http: any;
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
}
doBet() {
this.http.get('http://localhost:9000/db').success(function(data: any, status: any) {
this.bonus = data;
}
);
}
}
}
And this is how my view is set up:
<button ng-click="vm.doBet()">bet</button>
<div><span>bonus: {{ vm.bonus }}</span></div>
The view-model binding works properly when I change the bonus variable without the http request. However, when I attempt to update the bonus variable in the success function of the get request, I encounter the following error:
TypeError: Cannot set property 'bonus' of undefined
How can I successfully update variables in the success function? Any suggestions for a better or cleaner way to update data on requests would be greatly appreciated.