Here is my TypeScript/AngularJS controller:
class Some {
static $inject = ["$scope", "bookmarkService", "crawlerService"];
public allBookmarks;
constructor(protected $scope,
protected bookmarkService,
protected crawlerService)
{
$scope.allBookmarks = "+++";
this.$scope.allBookmarks = "***";
this.allBookmarks = "---";
this.getBookmarks();
}
public getBookmarks(): void
{
this.bookmarkService.getBookmarks()
.then(function(data) {
this.allBookmarks = data;
this.$scope.allBookmarks = data;
}, function() {
return "error";
});
}
First of all I get the error:
TypeError: Cannot set property 'allBookmarks' of undefined
I tried to debug this code and I see that the value I set in the constrictor is 'undefined' in the getBookmark()
method. I think the reason of the error: this
is the Object in constructor, but this
is Window in the getBookmark() method.
How can I fix code?