0

The problem is that after updating values in my form and then selecting the button to display the form again the values are sometimes there. I have tried to do this in different ways using a BehaviorSubject and also doing it with the EventEmitter. Any help would be appreciated.

I have attached the plunker: Plunker

Below is the data for my plunker example.

  this.units = [
  { 
    id: 1, 
    name: "Unit 1", 
    alarms: [
      { 
        name: "Alarm 1",
        description: ""
      },
      { 
        name: "Alarm 2",
        description: ""
      }
    ]
  },
  { 
    id: 2, 
    name: "Unit 2", 
    alarms: [
      { 
        name: "Alarm 1",
        description: ""
      },
      { 
        name: "Alarm 2",
        description: ""
      },
      { 
        name: "Alarm 3",
        description: ""
      }
    ]
  }
];

The user selects Unit 1 button and updates the description property of the first Alarm. The user clicks on the Unit 2 button and the data is save to the collection in the ngOnChange with the call to this.updateData(changedProp.previousValue); When the user click on button Unit 1 the value changed in the description of the first alarm is not always there.

I am also not sure if there is a better way of doing this.

Any help would be appreciated.

5
  • I have a github link to the one I tried with the BehaviorSubject: github.com/GertB1/test-forms Commented Apr 2, 2018 at 11:35
  • using previous value isn't good idea to save the unit Commented Apr 2, 2018 at 12:11
  • Hi Yerkon. I am using the previous value to get the id of the unit that I want to update. Then I use the form to extract the values that changed. Commented Apr 2, 2018 at 12:17
  • If I add a save button and remove the this.updateData(changedProp.previousValue); from the ngOnChanges then it works as expected. Is there a way to save automatically with out the save button? I am not sure if there is a life cycle hook that I can use? Commented Apr 2, 2018 at 13:11
  • I can solve your problem, but later✌ Commented Apr 2, 2018 at 13:45

1 Answer 1

1

Get access to child UnitEdit component from parent and when select the tab, call this.unitComp.updateData(unit) method from parent to save unit state:

export class App {
  units: Unit[];
  unit: Unit = null;
  constructor(private unitService: UnitService) {
  }

    @ViewChild(UnitEditComponent)
  private unitComp: UnitEditComponent;

  ...

  unitSelected(unit: Unit) {
    this.unitComp.updateData(this.unit);
    // save unit states
    console.log(this.unitComp)
    this.unit = unit;
  }
}

If you need more detail about @ViewChild decorator look at Parent calls an @ViewChild()

Plunker EXAMPLE

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Yerkon you are for-sure a master.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.