如何使用$ broadcast和$ on将产品添加到购物车中?

时间:2017-12-07 10:02:20

标签: javascript angularjs

  var count = 0;
  var found = false;
  $scope.addProduct = function (prodDetails) {
        angular.forEach($scope.cartProducts, function (newProduct, index) {

            var a = angular.equals(newProduct, prodDetails);
            found = true;
        });

        if (found) {
            prodDetails['qty']++;
        } else {
            prodDetails['qty'] = 0;
            $scope.cartProducts.push(prodDetails);
            count++;
        }

        console.log($scope.cartProducts);

        //  count++;

        $rootScope.$broadcast('addProduct-ToCart', $scope.cartProducts);
        console.log('check');

    }

// cartController

   $scope.cartItems = [];
   cartFactory.cartProducts().then(function (response) {

        $scope.cartItems = response.data;
      //  console.log(response.data);
        console.log($scope.cartItems);

      }, function (error) {
        console.error(error);
      });

    $rootScope.$on('addProduct-ToCart', function (event, data) {
      console.log('check');
    });

0 个答案:

没有答案