0

I have a component that I pass an Input variable(listType). On the component that I include this other component that needs an Input variable I have a function that I can set listType with different values based on the button they user click. This is working fine.

The issue I'm having is that I want to create a refresh button, that will basically just refresh the data with whatever value the user have it set to the (listType). So I created the following function on my parent component

refreshData() {
   this.listType = this.listType;
}

When I do this, nothing happens, the component that I'm passing listType does not call the ngOnChanges. Anything I need to do so the ngOnChanges detect that?

Ennio
  • 1,147
  • 2
  • 17
  • 34
  • but if you do like this `this.listType = this.listType;` nothing changes... it's like you're writing `let a = 5; a=a;` – Max Koretskyi Jun 22 '17 at 15:46
  • True :) Yeah that data didn't change. Is there a way I can call a function inside my child component? so I could make the refresh data call there? – Ennio Jun 22 '17 at 15:48
  • @Maximus even if it doesn't change what's inside `this.listType`, it should "technically" be firing the change detection from angular ngOnChanges and cause the child component to update. `this.listType.push()` or anything related to modification to nested objects wouldn't fire the change detection. – snaplemouton Jun 22 '17 at 15:53
  • @snaplemouton, are saying that parent and child components are not synchronized? whatever there is `this.listType` it should have been passed down to a child component – Max Koretskyi Jun 22 '17 at 15:54
  • Try [ngDoCheck](https://angular.io/guide/lifecycle-hooks#docheck) – snaplemouton Jun 22 '17 at 15:55
  • @Ennio, show your components code or create a plunker – Max Koretskyi Jun 22 '17 at 15:55
  • @Maximus I was probably a bit unclear and I apologize for that. What I meant is that changeDetection is done by Angular with dirty checking. Meaning that an array will essentially cause changeDetection to ignore any changes that happened to the objects inside the array because the array is essentially the same array. See [here](https://stackoverflow.com/questions/34796901/angular2-change-detection-ngonchanges-not-firing-for-nested-object) – snaplemouton Jun 22 '17 at 15:59
  • @snaplemouton, yeah, that's a different case. Angular checks by reference, that's correct. It's not clear what the OP wants :) – Max Koretskyi Jun 22 '17 at 16:00
  • I'm making a plunker here, but I was trying to get a function call I have ngOnChanges to be executed when the variable I have get its value updated. So if I have topicType = 'test' and later I click on a button and set topicType = 'test' the ngOnChanges does not detect that, but when I set to topicType = 'test123' it does. – Ennio Jun 22 '17 at 16:57
  • So here is the Plunker I have. https://plnkr.co/edit/7V4epQd8Gp1Q17sVXU0t So when you click on the buttons to set the text it works, you can see an alert, but once you click on the same button you do not see the alert again. – Ennio Jun 22 '17 at 17:22

0 Answers0