gpt4 book ai didi

javascript - 为什么 'this' 在此 TypeScript 片段中引用 'window'?

转载 作者:搜寻专家 更新时间:2023-10-30 21:05:56 27 4
gpt4 key购买 nike

给定这段代码:

module movieApp {
export interface IHomeControllerScope extends ng.IScope {
moviesToDownload: string[];
active: string;

deleteMovieFromDownloadList(movie: any);

markMovieAsDownloaded(movie: any);
}

export class HomeController {
public static $inject = [
'$scope',
'$location',
'MovieService'
];

constructor(private $scope: IHomeControllerScope, private $location: ng.ILocationService, private MovieService) {
this.$scope.$on('$locationChangeSuccess', (event) => {
this.setActiveUrlPart();
});

MovieService.getMoviesToDownload().then(response => {
this.$scope.moviesToDownload = response;
});
}

private setActiveUrlPart() {
var parts = this.$location.path().split('/');
this.$scope.active = parts[1];
}

public get moviesToDownload() {
return this.$scope.moviesToDownload;
}

public markMovieAsDownloaded(movie: any) {
movie.Downloaded = true;
}

public deleteMovieFromDownloadList(movie: any) {
this.MovieService.deleteMovieFromDownloadList(movie).then(() => {
debugger;
this.$scope.moviesToDownload = _.without(this.$scope.moviesToDownload, movie);
});
}
}
}

app.controller("HomeController", movieApp.HomeController);

一切正常,但在 deleteMovieFromDownloadList 方法中 this.$scope.movi​​esToDownload = _.without(this.$scope.movi​​esToDownload, movie);this 指的是 window 对象,而不是我期望的实际对象。

生成的 JavaScript 如下所示:

var movieApp;
(function (movieApp) {
var HomeController = (function () {
function HomeController($scope, $location, MovieService) {
var _this = this;
this.$scope = $scope;
this.$location = $location;
this.MovieService = MovieService;
this.$scope.$on('$locationChangeSuccess', function (event) {
_this.setActiveUrlPart();
});

MovieService.getMoviesToDownload().then(function (response) {
_this.$scope.moviesToDownload = response;
});
}
HomeController.prototype.setActiveUrlPart = function () {
var parts = this.$location.path().split('/');
this.$scope.active = parts[1];
};

Object.defineProperty(HomeController.prototype, "moviesToDownload", {
get: function () {
return this.$scope.moviesToDownload;
},
enumerable: true,
configurable: true
});

HomeController.prototype.markMovieAsDownloaded = function (movie) {
movie.Downloaded = true;
};

HomeController.prototype.deleteMovieFromDownloadList = function (movie) {
var _this = this;
this.MovieService.deleteMovieFromDownloadList(movie).then(function () {
debugger;
_this.$scope.moviesToDownload = _.without(_this.$scope.moviesToDownload, movie);
});
};
HomeController.$inject = [
'$scope',
'$location',
'MovieService'
];
return HomeController;
})();
movieApp.HomeController = HomeController;
})(movieApp || (movieApp = {}));

app.controller("HomeController", movieApp.HomeController);
//# sourceMappingURL=HomeController.js.map

可以看到,在生成的JS中,具体的方法使用了_this。这看起来不错,对吧?

有人可以向我解释发生了什么以及如何解决这个问题吗?

编辑:

我将它与 Angular 结合使用:

<body data-ng-app="movieApp" data-ng-controller="HomeController as homeCtrl">
<div class="col-sm-1">
<i class="fa fa-trash-o" data-ng-click="homeCtrl.deleteMovieFromDownloadList(m)" title="Verwijder uit lijst"></i>
</div>
</body>

编辑二:在尝试了所有建议然后设置回我在此处发布的原始代码后,一切正常!我不知道怎么回事,但我想这与 Chrome/VS 2013 有关。无论如何,感谢那些试图帮助我的人。

最佳答案

“deleteMovie...”功能可能绑定(bind)到按钮或其他 UI 元素。在这种情况下,此函数在窗口上下文中执行。要解决此问题,您应该在 Controller 的构造函数中定义函数体:

constructor(private $scope: IHomeControllerScope, private $location: ng.ILocationService, private MovieService) {
// other initialization code...

this.deleteMovieFromDownloadList = (movie: any) => {
this.MovieService.deleteMovieFromDownloadList(movie).then(() => {
debugger;
this.$scope.moviesToDownload = _.without(this.$scope.moviesToDownload, movie);
});
}
}

并在您的 Controller 类中声明适当的函数:

deleteMovieFromDownloadList: (movie: any) => void;

关于javascript - 为什么 'this' 在此 TypeScript 片段中引用 'window'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com