Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

angular - I have a button that down-votes a initial set number by 1 when clicked.But I want it to stop at 0

Below is my Quote class I have set the up-vote and down-vote properties to initial value of 0

export class Quote {
    showDescription: boolean;
    upvotes: number;
    downvotes: number;
    constructor(
    public content:string,
    public publisher:string,
    public author:string,
    public datePublished:Date
    ) {
        this.showDescription = false;
        this.upvotes =  0;
        this.downvotes = 0;
    }
}

Below is my HTML button

<li *ngFor = 'let quote of quotes; let i = index' quotes>
<div class="col-md-6 downvotes">
................

    <img src="assets/down-arrow.png" alt="" (click)="downVote(i)">
    <span >{{quote.downvotes}}</span>

...................
</div>
</li>

This how I'm calling the downvote function when button is clicked

import { Component, OnInit } from '@angular/core';
import { Quote } from '../quote'

@Component({
  selector: 'app-quote',
  templateUrl: './quote.component.html',
  styleUrls: ['./quote.component.css']
})

export class QuoteComponent implements OnInit {
............

  downVote(index:number) {
  this.quotes[index].downvotes -= 1;

.................

}
question from:https://stackoverflow.com/questions/66065323/i-have-a-button-that-down-votes-a-initial-set-number-by-1-when-clicked-but-i-wan

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
this.quotes[index].downvotes -= 1;
if (this.quotes[index].downvotes < 0) this.quotes[index].downvotes = 0;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...