2

I am going to create an online shopping site using angular 2. I got a cart in the navigation menu and i want to update the cart value when user add product in the cart. My can pass the value between component but can't update this value in the html template. My code example hear:-

nav.component

export class NavComponent implements OnInit {
  cartItem = 5;

  constructor(private cartService:CartServiceService) {
    this.cartItem = this.cartService.cartItem;
   }

}

nav template 

<nav class="navbar navbar-default">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Project name</a>
          </div>
          <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
              <li class="active"><a routerLink="/">Home</a></li>
              <li><a routerLink="/details">Details</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
              <li class="active"><a href="./">Cart <span>{{cartItem}}</span> </a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div><!--/.container-fluid -->
      </nav>

the navigation menu looks like enter image description here

I got a component call home component and home component have a button. when home component click to button the Cart menu automatically will update.

home component 

export class HomeComponent implements OnInit {

  cartItem: number;

  constructor(private cartService: CartServiceService) {
    
  }

  ngOnInit() {
  }

  click() {
    this.cartService.cartChange.subscribe((value) => {
      this.cartService.cartItem = 10;
    })
  }

}

And finally the service

cart service

@Injectable()
export class CartServiceService {

  constructor() {
    console.log(this.cartItem);
   }
  
  cartItem:number = 1;
  cartChange:Subject<number> = new Subject<number>();

  changeCart() {
    this.cartItem = 5;
    this.cartChange.next(this.cartItem);
  }

}

and i call this service in the app-module providers: [CartServiceService]

How can i update the cart in the navbar ??

6
  • Are you binding the changeCart() function to the button? can we see the code for that in the html? Commented Apr 27, 2017 at 6:18
  • changeCart() use in cart service. in home component template <button (click)="click()">text</button> Commented Apr 27, 2017 at 6:33
  • It should be <button (click)="changeCart()"> Commented Apr 27, 2017 at 6:36
  • i got an error ERROR TypeError: co.changeCart is not a function ...... Commented Apr 27, 2017 at 6:39
  • There's ya problem, it can't access the function to change the cart. I seem to remember something about having to create a parent class to share functions. try this stackoverflow.com/questions/36309837/… Commented Apr 27, 2017 at 6:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.