简体   繁体   English

TypeScript中带有绑定的Angular 1.5组件

[英]Angular 1.5 component in TypeScript with bindings

I have an Angular 1.5 component written in TypeScript and I'd like to know how to access the bindings in the controller. 我有一个用TypeScript编写的Angular 1.5组件,我想知道如何访问控制器中的绑定。

Here it is: 这里是:

export interface IMyController {
    myMethod: () => void;
}

class MyController implements IMyController {
    $onInit(): void {
    } 
    public myMethod(): void {
    }
}

angular.module('mymodule').component(
  'myCmp', {
      controller: MyController,
      controllerAs: 'vm',
      templateUrl: 'myCmp.component.html',
      bindings: {
          data: '=',
          label: '='
      }
});

Could anyone help me? 有人可以帮我吗?

They just assigned to your class object. 它们只是分配给您的类对象。 So try to put in component some data for bindings and then try to access. 因此,尝试在组件中放入一些用于绑定的数据,然后尝试访问。

class MyController implements IMyController {
 $onInit(): void {
   console.log(this.data, this.label);
 } 
 public myMethod(): void {
 }
}

angular.module('mymodule').component(
  'myCmp', {
    controller: MyController,
    controllerAs: 'vm',
    templateUrl: 'myCmp.component.html',
    bindings: {
      data: '=',
      label: '='
    }
});

I've just done that: 我已经做到了:

class MyController implements IMyController {
  public data: string;
  public label: string;
  $onInit(): void {
  } 
  public myMethod(): void {
  }
}

and it worked. 而且有效。

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

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