T
T
Turar Abu2015-11-20 16:19:03
JavaScript
Turar Abu, 2015-11-20 16:19:03

How to update controller in AngularJS?

I have a cart controller:

app.controller("cartItems", function($scope, $http){
  $http.get("/cart.json").success(function(data) { $scope.all = data });
});

and the HTML code of the cart itself:
<div class="item" ng-repeat="item in all">
      <i class="delete icon-trash" data-id="{{ item.id }}"></i>
      <img class="img" ng-src="{{ item.img }}" alt="">
      <p class="name">{{ item.name }}</p>
      <p class="info">{{ item.desc }}</p>

      <span class="price">{{ item.price }}
        <i class="icon-q"></i>
      </span>
    </div>

The shopping cart is a fixed block in the center of the screen. At the very beginning, he is invisible. When adding a single item to the cart, it sends an action to the server from the beginning. Next, it should update the data from $http, re-render the cart DOM, and show the cart block.
Question: How can I implement this on click?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kravchenko, 2015-11-20
@kemply

app.controller("cartItems", function($scope, $http){

  $scope.clickHandler = function(){
    $http.get("/cart.json").success(function(data) { $scope.all = data });
  }
});

<button ng-click="clickHandler()">Click me</buton>

V
Valeriy Donika, 2015-11-20
@Valonix

$scope.all.push(data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question