Search code examples
angularform-controlformarray

Angular Reactive Form - How to add subscribe valueChanges to a single control inside Form Control in Form Array


In Angular Reactive Forms I have form control "name" and form array control "skills", I can add valueChange subscribe to whole skills, but is there any way to add valueChange subscribe to "skillname" alone which is inside "skills".

this.exp = this.fb.group({
name: [null, Validators.required],
skills: this.fb.array([
  {
    skillname: 'java',
    experience: 2
  },
  {
    skillname: 'python',
    experience: 2
  }
]})})

Example:

this.exp.skills.valueChanges.subscribe(skills=>
          { //sample code });

But I need to listen to skillname:

this.exp.skills[0].skillname.valueChanges.subscribe(name=>
          { //sample code });

Like this, is there a way to do this?


Solution

  • you can use get and at functions.

    this.exp.skills.at(yourindex).skillname.valueChanges.subscribe(name=>
              { //sample code });
    

    would be

    this.exp.get('skills').at(yourIndex).get('skillname').valueChanges.subscribe(name=> {});