【问题标题】:Angular 2: What are @input and @output propertiesAngular 2:什么是@input 和@output 属性
【发布时间】:2016-04-25 16:31:03
【问题描述】:

在@component 中,

@input 和 @output 属性代表什么以及它们的用途是什么?

什么是指令以及为什么我们必须将指令放在下面的结构中?

directives:[CORE_DIRECTIVES, FORM_DIRECTIVES]

如果有人能告诉我@input 和指令之间的区别,那就太好了。

【问题讨论】:

    标签: input output angular


    【解决方案1】:

    简而言之:

    @Input 用于组件标签的属性。

    @Output 用于组件标签的事件。

    想想 HTML 输入:

    <input type="button" onclick="doSomething($event)" ></input>

    输入的type 属性告诉组件它将如何呈现和表现,作为一个按钮,作为一个文本输入等等......如果你要使用类似的属性,你会使用@Input注释,因为您想在组件中插入信息。

    输入的onclick 属性/事件在Angular 2 中被视为@Ouput 属性,因为它将信息作为$event 对象发送给可能使用它的外部组件。

    见: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#inputs-outputs

    就像赤壁提到你不再这样做:

    directives:[CORE_DIRECTIVES, FORM_DIRECTIVES]

    为了使用它们。

    【讨论】:

    • 当你想在 Angular 2 中使用指令时,你必须指定定义这些指令的组件。 CORE_DIRECTIVES 和 FORM_DIRECTIVES 是指令列表,因此您可以一次包含它们。我的猜测是 CORE_DIRECTIVES 用于包含核心功能指令,如 ngFor、ngIf、ngModel 等,而 FORM_DIRECTIVES 包含诸如 ngControl、ngForm 等指令。但同样,因为 Beta它们不再有用了。
    【解决方案2】:

    您不再需要使用它们,这是 Alpha 版本上使用的旧模型。现在您只需将它包含在 import 语句中就足够了。

    查看这个使用 beta 编写的教程,您会发现 CORE_DIRECTIVES 和 FORM_DIRECTIVES 不再使用。

    Angular One Framework Tutorial

    Angular 2 Developer Guide for Forms

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2016-04-09
      • 2018-10-02
      • 2017-03-28
      • 2016-12-30
      • 2018-11-17
      • 1970-01-01
      相关资源
      最近更新 更多