how i can update ng-content by updating selector on content elements?
I have code in app.component.html:
<my-slider>
<img src="../../assets/logo1.png" />
<img src="../../assets/logo2.png" />
</my-slider>
Here's the code my-slider.component.ts:
@Component({
selector: 'my-slider',
template: '<ng-content select=".render" ></ng-content>
<button (click)="render()"> Show</button>',
styleUrls: ['./slider.component.css']
})
export class SliderComponent {
@ContentChildren(ImgComponent) img: QueryList<ImgComponent>;
constructor() { }
render() {
this.img.last.show();
}
}
And ImgComponent.ts
@Component({
selector: 'img',
template: '',
})
export class ImgComponent {
render: boolean = false;
constructor(
private elRef: ElementRef,
private renderer: Renderer2
) { }
show() {
this.render = true;
this.renderer.addClass(this.elRef.nativeElement, 'render');
}
hide() {
this.render = false;
this.renderer.removeClass(this.elRef.nativeElement, 'render');
}
}
When I click the button, a class is added to the element, but ng-content never updates. Can i somehow make it work? Or how I can filter components in ng-content?
Thank you, all for your responce. Here is solution i had been looking for:
I had added a structual directive to img instead of making it angular component and now app.component.html looks like:
my-slider.component.ts is not realy changed. I just remove
select='.render'
anyway here it is:And finaly my structual directive ImgDirective:
Here live example: link