What is the analogue of the property of the "controllerAs" directive in an Angular 2 component?

I started migrating one of my Angular 1 directives to the Angular 2 component.

The directive in which I am currently included has the controllerAs: 'ctrl' property, and the directive template uses the prefix 'ctrl.' when accessing properties.

Looking at the official component ComponentMetadata I do not see any properties that can be used instead.

+5
source share
1 answer

There is no controllerAs equivalent in Angular 2. For example, given this class and the controller pattern:

 @Component({ selector: 'component-a', template: `<div class="component-a"> <div class="counter" (click)="increment()">Component A: {{counter}}</div> </div>` }) export class ComponentA { counter = 0; increment() { this.counter += 1; } } 

In the increment() method, this limited to the controller instance of this component itself. In the template, access to the counter can be obtained through {{counter}} .

As we can see, there is no mechanism for specifying the controller, because we can already access it using the default functionality.

You might think that the controllerAs mechanism was integrated into the functionality of the default Angular 2 component or this function was removed as it is no longer needed, depending on how you look at it.

+9
source

Source: https://habr.com/ru/post/1241147/


All Articles