Sarvesh Yadav
Sarvesh Yadav

Reputation: 2620

Passing value from Parent component to child component for ngForm in angular2?

I want to pass input value from Parent component () to child component ().(I tried with @input but its not working. I think I am making some mistake?)

parent component :

<value>
<search [vin]="vin"></search><-- child componnent 
<input class="in" type="text" pInputText [ngFormControl]="finVin" [ng-model]="vin"/>
</value>
class demo{
@Input() private vin:string;
}

child component :

 <search>
    <div class="lbl">
        {{vin}}

    </div>
    </search>

Upvotes: 1

Views: 392

Answers (2)

micronyks
micronyks

Reputation: 55443

NOTE: [ng-model] is changed to [ngModel].

<value>
   <search [vin]="vin"></search><-- child componnent 
   <input class="in" type="text" pInputText [ngFormControl]="finVin" [ngModel]="vin"/>
</value>

class demo{
   vin="Angular2";
}

<search>
    <div class="lbl">
        {{vin}}

    </div>
</search>


export class search{
     @Input() private vin:string;

     ngOnChanges(...args:any[]){
           //console.log(args[0].vin); //for previous and current value.
      }
}

Upvotes: 1

Maximilian Riegler
Maximilian Riegler

Reputation: 23516

It looks like you have it the wrong way round. The @Input annotation is needed in the child component, so Angular 2 knows you're passing something into it.

So remove the @Input in your parent component and add it to your child component:

class search {
    @Input() private vin:string;
}

Upvotes: 0

Related Questions