javascript - AngularJS、Typescript 通过服务传递变量

标签 javascript angularjs

我目前正在使用 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/

相关文章:

javascript - Vue - 在方法中过滤数组时传递过滤条件

angularjs - asyncValidator 依赖于其他字段的值

javascript - 使用事件的轨道链接 - 竞赛条件

javascript - 如何在选中时打印复选框的名称?

angularjs - parseFloat 在 angularjs View 中不起作用

javascript - 根据一系列字符串条件删除行

javascript - 如何在没有任何性能问题的情况下渲染计时器

javascript - 在 AngularJS 中用 div 包裹图像

javascript - 基本的 javascript 流程 - 在各种浏览器中

javascript - Angular ng-click 不会触发我指令中的函数