【问题标题】:Angular 1.5 component in TypeScript with bindingsTypeScript 中带有绑定的 Angular 1.5 组件
【发布时间】:2017-01-24 11:42:01
【问题描述】:

我有一个用 TypeScript 编写的 Angular 1.5 组件,我想知道如何访问控制器中的绑定。

这里是:

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: '='
      }
});

谁能帮帮我?

【问题讨论】:

    标签: angularjs typescript binding


    【解决方案1】:

    它们只是分配给您的类对象。所以尝试在组件中放入一些数据进行绑定,然后尝试访问。

    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: '='
        }
    });
    

    【讨论】:

      【解决方案2】:

      我刚刚做到了:

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

      它成功了。

      【讨论】:

        猜你喜欢
        • 2016-09-10
        • 1970-01-01
        • 2016-09-12
        • 2016-07-25
        • 2016-07-10
        • 2016-10-21
        • 2017-03-06
        • 2017-05-10
        • 2016-07-03
        相关资源
        最近更新 更多