click the button and increase number through component - angular -
hej, i'm having having problem button should increase number +=1 , display in view number.
app.component.ts
import { component } '@angular/core'; import { counterservice } '../common/services/counter.service'; @component({ selector: 'app-root', templateurl: './app.component.html', styleurls: ['./app.component.sass'] }) export class appcomponent { constructor(private counterservice: counterservice) {} count() { return this.counterservice } set count(count){ this.counterservice.count += 1; } }
counter.service
export class counterservice { count = 0; }
app.component.html
<div class="container"> <div> <p> {{ counterservice.count }}</p> <button (click)="count()" class="btn btn-default form-control increasebtn">increase</button> </div> </div>
i can display 0 when i'm stacked incrementation. thx in advance!
try it:
public getcount() { return this.counterservice.count } public inccount(){ this.counterservice.count += 1; }
in html:
<button (click)="inccount()" class="btn btn-default form-control increasebtn">increase</button>
and in counter.service:
export class counterservice { public count = 0; }
but better manage variables in service
Comments
Post a Comment