Angular 1.5 component in TypeScript with bindings

Question!

I have an Angular 1.5 component written in TypeScript and I'd like to know how to access the bindings in the controller.

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?



Answers

I've just done that:

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

and it worked.



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


This video can help you solving your question :)
By: admin