0

In Angular 2, is it possible to use a ngModel as somekind of: " 2 one-way data bindings"?

I'm trying to achieve an import from the users LinkedIn as suggestions that should then be posted with a form to another model.

In the angular docs it is stated that:

Inside [(ngModel)] We could have achieved the same result with separate bindings to the input element's value property and input event.

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

I figured that maybe there would be a way to use ngModel with the [value] and the (input) as two separate data flows, meaning that, an import is made from a user model and then posted to a company model.

The following is a snippet from my code:

<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>

For example, my previous form code looked like this: (Without any imports or anything. The underlying post-request I have is working just fine and a new company is submitted to my database.)

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

Some typescript code:

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

My node app on the server crashes when the actual http request is made, and of course this might not be at all possible to achieve with ngModel but I don't know so I'm starting here. In other words... Must the ngModel be bound to only one model in angular 2 or can I customize it to my needs to import data and send this data to another model? I hope this makes sense.

So basically my question is: Is it some smooth way to use ngModel to do this kind of data binding or am I way of with this?

Also: I'm able to get the correct data from my "users" model when importing from LinkedIn.

Anton Scotte
  • 439
  • 5
  • 19

1 Answers1

1

I don't really understand your question. Maybe you are looking for

[ngModel]="company?.industries" (ngModelChange)="company.industries = $event" 
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I'm simply trying to get the value from the form input to be submitted along with my form using ngModel. When changing up the ngModel's [value] to import a value from another place (like linkedIn) it makes the app crash on the server. (As opposed to binding ngModel only to company.industries and neglecting the whole linkedIn part) I'll look into ngModelChange and see if it might point me in the right direction, thanks! – Anton Scotte Aug 29 '16 at 19:34