【问题标题】:What's the difference between [ngModel] [(ngModel)] and ngModel [duplicate][ngModel] [(ngModel)] 和 ngModel [重复] 有什么区别
【发布时间】:2021-05-08 16:22:02
【问题描述】:

[(ngModel)] 是从组件到视图和从视图到组件交换数据的两种方式绑定。 [ngModel] 是一种方式绑定,但它给了我与 [(ngModel)] 相同的行为。

使用 [ngModel],我可以从组件中获取值并将其显示在视图中,我也可以将值从视图设置到组件。如果 [ngModel] 是单向绑定,我们如何在组件和视图之间交换数据。我真的对这两个注释感到困惑。

另一个问题,ngModel 是与 ngform 一起使用的单向绑定吗?

  <input class="form-control" type="text" name="designation" [ngModel]="currentProduct.name"> 

给出相同的东西:

<input class="form-control" type="text" name="designation" [(ngModel)]="currentProduct.name"> 

【问题讨论】:

    标签: angular typescript


    【解决方案1】:
    1. [ngModel]="currentProduct.name" 是单向绑定的语法

    2. [(ngModel)]="currentProduct.name" 用于双向绑定

    这意味着在第一种语法中,您可以将数据从父组件传递给您的子组件。

    第二个语法你可以通过两种方式传递数据,父母给孩子,孩子给父母。

    阅读更多来自angular doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 2018-02-17
      • 2017-08-31
      • 1970-01-01
      • 2017-11-24
      • 2014-11-04
      • 2020-03-06
      相关资源
      最近更新 更多