For example, we have two images, one is in parent component, the other is in child component. Click child image can make the parent image changes, which is easy to implement. But how can I change child image when click parent image?
3 Answers
Your plunker doesn't work so it's a little hard to figure out where your specific problem is. But the general case isn't too hard.
If you want to make the parent change when the child is clicked, you just need to use an output
on the child. For example - clicking the child here alternates the image in the parent (working plunk)
import {Component, Output, EventEmitter} from 'angular2/core'
@Component({
selector: "child";
template:`
<div>
<h3> Child Image: </h3>
<img src="{{uri}}" (click)="imageClicked($event)" />
</div>
`
})
export class childComponent {
@Output() wasClicked: EventEmitter<Node> = new EventEmitter();
uri: string = "https://upload.wikimedia.org/wikipedia/commons/e/e5/Элемент_И-НЕ_%28100%29.PNG"
imageClicked(e) {
console.log("child component recieved click" )
this.wasClicked.emit(e)
}
}
@Component({
selector: "parent";
template:`
<div>
<h2> Parent Image: </h2>
<img src="{{uri}}" />
</div>
<child (wasClicked)="childClicked(e)"></child>
`,
directives: [childComponent]
})
export class parentComponent {
switch: boolean = false;
uri: string = "https://upload.wikimedia.org/wikipedia/commons/1/14/Элемент_Исключающее_ИЛИ_%28100%29.png"
childClicked(e){
console.log("parent recieved click event from child")
this.switch
? this.uri = "https://upload.wikimedia.org/wikipedia/commons/1/14/Элемент_Исключающее_ИЛИ_%28100%29.png"
: this.uri = "https://upload.wikimedia.org/wikipedia/commons/7/7a/Элемент_ИЛИ-НЕ_%28100%29.PNG"
this.switch = !this.switch
}
}

- 90,562
- 7
- 108
- 148
I would see several ways to do that:
- Create a custom event (using
@Output
) in the child component and fires the event to notify the parent component. See this link for more details: `Error: Output is not defined` when passing value outside a directive to parent directive - Inject the parent component instance into the child one. From the child component, directly update the property used for the image in the parent. See this answer for more details: Angular2 Exception: No provider for Service
- Use a shared service with an EventEmitter property to make communicate parent and child components. See this link for more details: Delegation: EventEmitter or Observable in Angular2

- 1
- 1

- 198,364
- 44
- 396
- 360
-
Thanks Thierry. Sorry I made the wrong question. I know how to change parent image when click child image using outputs. My question is actually: how to change child image when click parent image? I have tried using outputs, it didn't work. – Ng2-Fun Feb 19 '16 at 14:38
-
I think that @Query could help you to reference the child image from the parent component. This way you will be able to update it when clicking in the parent one. – Thierry Templier Feb 19 '16 at 14:48
-
See this link for more details: https://angular.io/docs/ts/latest/api/core/Query-var.html – Thierry Templier Feb 19 '16 at 14:49
how to change child image when click parent image?
In general, when a parent wants to communicate something to a child, use a child input property. If you want to execute some logic, implement ngOnChanges()
to be notified when an input property changes:
ngOnChanges(changes) {
console.log(changes);
}
I assume you want to do something in PauseBtnComponent when input propertyplayBtnStatus
changes:
ngOnChanges(changes) {
console.log(changes);
if(changes.playBtnStatus.currentValue) { ... }
else { ... }
}
Another option is to use @ViewChild
, @ViewChildren
, or @Query
in the parent component to get references to the child(ren). Then you can call public method(s) on the child.

- 362,217
- 114
- 495
- 492
-
Thanks. I changed the code a little bit to change child image however it doesn't worlk. ngOnChanges(changes) { console.log(changes.playBtnStatus.currentValue) this.pauseEnabledImg = this.pauseEnabledImg; } – Ng2-Fun Feb 19 '16 at 17:04
-
@J.Fun, your comment doesn't make sense: `this.pauseEnabledImg = this.pauseEnabledImg;`. You need to check the value of `changes.playBtnStatus.currentValue` and conditionally change your image based on the value. – Mark Rajcok Feb 19 '16 at 17:09
-
Sorry I am not very clear about it. I assume after mouse click the parent image, its status should be changed from enable to disable, then it will call the onChange function and let the child image be enabled. However it doesn't work as I expected. Could you please show me on the plunker? (the state of parent image and child image is always opposite so that one can trigger the state of another and vice verse) – Ng2-Fun Feb 19 '16 at 18:52
-
@J.Fun, I updated the plunker to include `if(!changes.playBtnStatus.currentValue) { this.currentPauseImg = this.pauseEnabledImg; }`. See if that's what you need. – Mark Rajcok Feb 19 '16 at 19:16