简体   繁体   English

Typescript接口语法在控制器中不起作用

[英]Typescript interface syntax not working in controller

I have created a pageSettings interface 我已经创建了pageSettings界面

module app.common {
  export interface IPageSettings {
    pageTitle : string;
    pageDesc  : string;
  }

  export class PageSettings implements IPageSettings {
    constructor(public pageTitle : string,
                public pageDesc  : string
    ) {
    }
  }
}

In the controller i am then calling that interface 然后在控制器中调用该接口

module app.home {
interface IKeysModel {
    pageSettings: app.common.IPageSettings;
}

class KeysCtrl implements IKeysModel {
    pageSettings: app.common.IPageSettings;

    static $inject = [];
    constructor() {
        var vm = this;
        vm.pageSettings.pageTitle = "Keys",
        vm.pageSettings.pageDesc = "List of your keys"
    }
  }
  angular.module("app").controller("keysCtrl", KeysCtrl);
}

This code does not work. 此代码不起作用。 However if i change the call to the interface properties to this 但是,如果我将对接口属性的调用更改为此

vm.pageSettings = {
     pageTitle: "Keys",
     pageDesc : "List Of Your Keys"
}

Then is works fine. 然后工作正常。 Im just wondering why? 我只是想知道为什么? I feel like the first assignment should work. 我觉得第一个作业应该有效。 Im fairly new to typescript, just trying to understand the language. 我对打字稿还算陌生,只是想了解这种语言。

For further context . 供进一步了解。 This is the view 这是视图

<div class="page-title">
  <h2>{{::vm.pageSettings.pageTitle}}</h2>
  <h6>{{::vm.pageSettings.pageDesc}}</h6>
</div>

I think you should initialize variable vm.pageSettings: 我认为您应该初始化变量vm.pageSettings:

vm.pageSettings = <app.common.IPageSettings>{};

before setting it's fields: pageTitle and pageDesc 在设置字段之前:pageTitle和pageDesc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM