【问题标题】:I am trying to make a simple shopping cart but im struck at adding products to cart?我正在尝试制作一个简单的购物车,但我无法将产品添加到购物车?
【发布时间】:2017-07-24 09:42:30
【问题描述】:

我是 angular 新手,我正在尝试开发一个简单的购物车,我刚刚创建了产品列表,但我不知道如何将它们添加到购物车?

这是我的插件链接:https://plnkr.co/edit/oo05d6H6AxuJGXBAUQvr?p=preview

谁能告诉我如何让我的代码对用户更有效?

我们将不胜感激任何形式的帮助

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
 <script src="script.js"></script>
<body ng-app="myApp" ng-controller="mobileController">
<h2> Welcome to Mobile Store</h2>  

<p>Search:<input type="text" ng-model="test"></p>


<ul>
 <li ng-repeat="item in items|filter:test"><a href="#/item/{{item.name}}">{{ item.name }}</a>
  </li>

</ul>



<div ng-view></div>

  </body>

</html>
// Code goes here



var app = angular.module("myApp", ["ngRoute"]);
app.controller('mobileController', function($scope) {
  $scope.items = [{
    name: 'Iphone',
    price: 70000,
    rating: '*****',
    image: 'http://i.imgur.com/hfMaGTN.png'
  }, {
    name: 'Oneplus',
    price: 60000,
    rating: '****',
    image: 'http://i.imgur.com/sBcs5ZE.jpg'
  }, {
    name: 'Samsung',
    price: 50000,
    rating: '***',
    image: 'http://i.imgur.com/8Bexxbf.jpg'
  }, {
    name: 'Sony',
    price: 40000,
    rating: '***',
    image: 'http://i.imgur.com/0c7PcX8.png'
  }, {
    name: 'Moto',
    price: 20000,
    rating: '****',
    image: 'http://i.imgur.com/HyWR1VE.png'
  }];
});
app.config(function($routeProvider) {
  $routeProvider
    .when('/item/:itemName', {
      templateUrl: 'details.html',
      controller: 'ItemCtrl'
    });
});

app.controller('ItemCtrl', ['$scope', '$routeParams',
  function($scope, $routeParams) {
    angular.forEach($scope.items, function(item) {
      if (item.name == $routeParams.itemName) {
        $scope.itemName = item.name;
        $scope.itemPrice = item.price;
        $scope.itemRating = item.rating;
        $scope.itemImage = item.image;
      }
    });
  }
]);

This is the details page
<!DOCTYPE html>

 <p>ItemName: {{itemName}}</p>
<p> ItemPrice: {{itemPrice}}</p>
 <p>ItemRating:{{itemRating}}</p>
<img src="{{itemImage}}">
<p><input type = "submit" value = "Add to Cart" ng-click = "addProduct()"></p>

我应该在同一个 ItemCtrl(controller) 中包含 addProduct 函数吗?

【问题讨论】:

  • 你在哪里写过 addProduct() 代码??

标签: jquery html angularjs angular-ui-router


【解决方案1】:

看看这个链接,如果不是很好,它可能有你要找的东西我试过了,哈哈

https://www.codeproject.com/Articles/1034593/Angular-js-with-ASP-NET-MVC-Insert-Update-Delete

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多