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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -