转换angular 1的问题.将x控制器导入TypeScript

Issues converting angular 1.x controller to TypeScript

本文关键字:控制器 导入 TypeScript 问题 angular 转换      更新时间:2023-09-26

TypeScript新手,尝试从js转换为ts(使用angular 1.x)。这是一个控制器。在转换为ts时有问题。"module"是ui-router状态提供商中的一个自定义数据元素。

(function(module) {
    "use strict";
    module.controller("aboutController", [
        "$state",
        function($state) {
            var vm = this;
            // pass the module to the service to get the content for the about page
            // $state.current.data.module
            vm.content = $state.current.data.module;
        }
    ]);
} (angular.module("app.ui")));

我最近一次尝试使用ts:

module myApp.ui {
    "use strict";
    class AboutController {
        constructor(private $state: ng.ui.IStateProvider) {
            this.content = $state.current.data.module;
        }
    }
    angular.module("app.ui").controller("aboutController", AboutController);
}

:
错误TS2339:属性"content"不存在类型"AboutController"。
错误TS2339:属性'current'不存在类型'IStateProvider'.

<<p> 修复/strong>:
(1)我应该一直使用IStateService,而不是IStateProvider——解决了上面#2的"当前"bug。(2)在actor上面添加一行"public content: string;"——解决上面#1的"content"错误。Thx @Brocco

属性'content'在类型'AboutController'上不存在

你需要在TypeScript中声明所有类属性:

class AboutController {
    content: any; // property declaration
    constructor(private $state: ng.ui.IStateProvider) {
        this.content = $state.current.data.module;
    }
}

属性'current'在类型' istateprovider '上不存在

使用IStateService。注意:这些通常是你注入的服务,后缀是一致的。