是否可以使用2个单向数据绑定自定义Angular 2的NgModel数据流?

时间:2016-08-29 18:29:04

标签: angular angular2-ngmodel

在Angular 2中,是否可以使用ngModel作为某些人:" 2个单向数据绑定"?

我试图从用户LinkedIn获取导入作为建议,然后将表单发布到另一个模型。

angular docs中声明:

  

内部[(ngModel)]我们可以通过单独绑定到input元素的value属性和输入事件来获得相同的结果。

<!--From angular.io-->
<input [value]="currentHero.firstName"
       (input)="currentHero.firstName=$event.target.value" >

我认为可能会有一种方法将ngModel与[value]和(input)一起用作两个独立的数据流,这意味着,导入是从用户模型和然后发布到公司模型。

以下是我的代码中的代码段:

<div class="form-group">
    <label for="industry">Industry</label>
    <input type="text" class="form-control" 
        [value]="user.linkedIn.positions.values[0].company.industry" 
        (input)="company.industries=$event.target.value" 
        name="industry" required>
</div>

例如,我之前的表单代码如下所示: (没有任何导入或任何东西。我的基础后请求工作正常,并且新公司被提交到我的数据库。)

<input type="text" 
    class="form-control" 
    [(ngModel)]="company.industries" 
    name="industry" required #name="ngModel">

一些打字稿代码:

getCurrentUser() {
this.userService
  .getCurrentUser()
  .then(user => this.user = user)
  .catch(error => this.error = error);
}

submitted = false;
  onSubmit() { 
    this.submitted = true;
    this.companyService
      .save(this.company)
      .then(company => {
        this.company = company;
        this.goBack(company);
      })
      .catch(error => this.error = error);
  }

服务器上的节点应用程序在发出实际的http请求时崩溃,当然可能根本不可能使用ngModel实现但是我不知道所以我&# 39;从这里开始。换句话说......必须将ngModel绑定到角度为2的一个模型,还是可以根据我的需要自定义导入数据并将此数据发送到另一个模型?我希望这是有道理的。

所以基本上我的问题是:使用ngModel进行这种数据绑定是一种流畅的方式还是我的方式?

另外:从LinkedIn导入时,我能够从&#34;用户&#34; 模型中获取正确的数据。

1 个答案:

答案 0 :(得分:1)

我真的不明白你的问题。也许你正在寻找

[ngModel]="company?.industries" (ngModelChange)="company.industries = $event"