使用Typescript时如何访问HTML中的类变量?

时间:2015-03-30 16:46:34

标签: angularjs

我的控制器和HTML中有以下代码:

class AccessWebApiController {

    method;
    response;
    statusCode;
    tab = {
        selected: null
    };

    static $inject = [
        '$http',
        '$scope',
        '$sce'
    ];
    constructor(
        public $http: ng.IHttpService,
        public $scope,
        public $sce: ng.ISCEService
        ) {
        $scope = this;
        this.tab.selected = 100;
    }

} 

<main ng-controller="accessWebApiController"
      id="accessContentPage">   
    xxxx {{ tab }} xxxx 
</main> 

所有内容似乎都被正确访问但页面显示并且在xxxx和xxxx之间没有显示任何内容

有什么简单的我做错了吗?

1 个答案:

答案 0 :(得分:0)

$ scope =这绝对不对(最新的打字稿编译器将其标记为问题)。但是,我认为有些可能会受益于更完整的示例,使用控制器作为语法并注册模块和控制器。我有一个plunkr,我把打字稿放在那里,但它实际上没有做任何事情,所以我也会把打字稿放在这里。

Plunkr:http://plnkr.co/edit/uFuB04SUjWXYPwOSCAIW?p=preview

代码:

class Tab {
    public selected: number;
}

class AccessWebApiController {

    public method: any;
    public response: any;
    public statusCode: any;
    public tab: Tab;

    static $inject = [
        '$http',
        '$sce'
    ];
    constructor(
        public $http: ng.IHttpService,
        public $sce: ng.ISCEService
        ) {
        this.tab = {
            selected: null
        };
        this.tab.selected = 100;
    }

} 

var app = angular.module("WebApiModule",[]);

app.controller("accessWebApiController", ($http: ng.IHttpService, $sce: ng.ISCEService) => new AccessWebApiController($http,$sce));