K
K
kikosko2018-10-22 16:04:17
Angular
kikosko, 2018-10-22 16:04:17

How to change the value of a variable in a separate element?

When the toggle condition in the toggleDate method fires, I also want to change the value of the "format" variable in a single element, now when clicked, the date format of all elements is changed. How to implement it?

<div class="col-lg-3 col-md-4 col-6" *ngFor="let pic of collection; ">
        <div class="info">
                <img [src]="pic.url" alt="test" class="img-responsive">
                <p class="lead"><span>{{pic.id}}:</span>{{pic.title}}</p>
                <p class="more-info-text">Orc birthday: {{bDay | date: format}}</p>
        </div>
        <div class="card buttons">
            <button class="btn btn-info" (click)="toggleDate(pic.id)">{{toggDate[pic.id] ? 'Full date' : 'Short date'}}</button>
        </div>
    </div>

export class GalleryComponent implements OnInit {
    bDay: object = new Date();
    toggDate: any = [];
    format: string;

    constructor() {}

    toggleDate(picId: number) {
        this.toggDate[picId] = !this.toggDate[picId];
        if (this.toggDate[picId]) {
            this.format = 'shortDate';
        } else {
            this.format = 'fullDate';
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-10-22
@kikosko

Replace
with
And the format property can be removed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question