2016-02-22 92 views
0

我目前正在使用AngularJS开发一个应用程序,现在我需要传递一个URL,当我点击菜单按钮时,我可以在iframe中使用该URL在另一个视角与另一个控制器。我已经尝试了很多东西,并通过跑起来像全#1,但我无法找到我的问题的解决方案..AngularJS,Typescript通过服务传递变量

我的服务:

module Services { 
    export class PassUrlService { 

     getUrl; 
     setUrl; 
     givenUrl; 

     constructor($scope) { 
      this.getUrl = function() { 
       return this.givenUrl; 
      } 

      this.setUrl = function (value: string) { 
       this.givenUrl = value; 
      } 
     } 

    } 
} 

我的控制器:

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; 
     } 

    } 
} 

我得到的错误是:

错误TS2339:类型'typeof PassUrlService'上不存在属性'setUrl'。

我希望有人能帮助我解决这个问题,提前致谢!

回答

2

我想你是不是注射的服务控制器,尝试用下面

service: Services.PassUrlService 

constructor($scope, $sce, service: Services.PassUrlService) { 
     $scope.data = this.data; 
     $scope.vm = this; 
     this.sce = $sce; 
     this.service = service 
    } 
样本