I have created a component that looks like this:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {
constructor() { }
@Input() text: string
@Input() imgSrc: string
@Input() reveal: boolean
ngOnInit() {
}
}
The HTML for this component is:
<span class="my-span" [ngClass]="reveal ? 'is-active' : '' ">
{{ text }}
<img src="{{ imgSrc }}" alt="">
</span>
And CSS:
.my-span {
cursor: pointer;
text-decoration: underline;
text-decoration-style: dotted;
img {
max-width: 200px;
object-fit: contain;
pointer-events: none;
opacity: 0;
}
&.is-active {
img {
pointer-events: all;
opacity: 1;
}
}
}
The basic idea of what I would like to do is to use the component something like:
<my-component (click)="clickTest= !clickTest" reveal="clickTest" text="some text" imgSrc="images/my-image.jpeg"></my-component>
So by clicking the component on where ever I have placed it should toggle through some boolean (on/off), which activates the is-active class in the component, i.e. displaying the image in this case.
The text and imgSrc part works, but the click thing doesn't. First of all, in my mind it doesn't make sense that I have to create a new boolean variable for each it on the page where I want this component present. That kind of negates the idea of a component in my head, since the boolean should be WITH the component, and not where the component is called (which could be several places).
So I would think I just need to somehow check when the component is clicked, and then this click event is sent to the component, which can then translate it into and on/off signal.
But I am not quite sure how to achieve this?