Angular 2组件中'controllerAs'指令的属性的模拟是什么? [英] What is the analog of the 'controllerAs' directive's property in Angular 2 component?

查看:46
本文介绍了Angular 2组件中'controllerAs'指令的属性的模拟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始将Angular 1指令之一迁移到Angular 2组件.

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

我当前使用的指令具有controllerAs: 'ctrl'属性,并且该指令的模板在访问属性时使用'ctrl.'前缀.

The directive I am currently on has the controllerAs: 'ctrl' property and the directive's template uses 'ctrl.' prefix when it access properties.

查看官方ComponentMetadata文档没有看到可以代替此属性使用的任何属性.

Looking at the official ComponentMetadata doc I do not see any properties that can be used instead of this one.

推荐答案

在Angular 2中没有等效于controllerAs的示例.例如,给定以下控制器类和模板:

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

@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;
    }

}

在方法increment()中,this绑定到该特定组件本身的控制器实例.在模板中,可以通过{{counter}}访问计数器.

In the method increment(), this is bounded to the controller instance of that particular component itself. In the template, the counter can be accessed via {{counter}}.

我们看到没有命名控制器的机制,因为我们已经可以使用默认功能对其进行访问.

As we can see there is no mechanism to name the controller because we can already access it using the default functionality.

您可以认为controllerAs机制已集成在Angular 2的默认组件功能中,或者已不再使用该功能,具体取决于您的外观.

You can either think that the controllerAs mechanism has been integrated in the default component functionality of Angular 2, or that functionality has been removed as its no longer needed, depending on how you look at it.

这篇关于Angular 2组件中'controllerAs'指令的属性的模拟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆