我目前正在使用 AngularJS 开发一个应用程序,现在的问题是,当我单击菜单按钮时,我需要传递一个 URL,以便我可以在另一个 Controller 的另一个 View 中的 iframe 中使用该 URL。我已经尝试了很多事情,并且像整个 Stackoverflow 一样运行,但我找不到解决我的问题的方法..
我的服务:
module Services {
export class PassUrlService {
getUrl;
setUrl;
givenUrl;
constructor($scope) {
this.getUrl = function() {
return this.givenUrl;
}
this.setUrl = function (value: string) {
this.givenUrl = value;
}
}
}
}
我的 Controller :
module Controllers {
export class MainController {
data = [];
sce;
IframeUrl;
constructor($scope, $sce) {
$scope.data = this.data;
$scope.vm = this;
this.sce = $sce;
}
setIframeUrl = function (url) {
this.IframeUrl = Services.PassUrlService.setUrl(this.sce.trustAsResourceUrl(url));
debugger;
}
}
}
我收到的错误是:
error TS2339: Property 'setUrl' does not exist on type 'typeof PassUrlService'.
希望有人能帮我解决这个问题,先谢谢了!
最佳答案
我认为您没有在 Controller 中注入(inject)服务,请尝试使用下面的示例
service: Services.PassUrlService
constructor($scope, $sce, service: Services.PassUrlService ) {
$scope.data = this.data;
$scope.vm = this;
this.sce = $sce;
this.service = service
}
关于javascript - AngularJS、Typescript 通过服务传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35550492/