Giter VIP home page Giter VIP logo

chi-shopping-cart's Introduction

Chi Shopping Cart

This is a sample angular application that uses two controllers and a factory. The InputController takes in product information that is stored in the ShoppingService factory. The CartController displays the products added.

MVC Overview

Controllers and views get reset when you navigate from page to page. We need a way to persist data. MVC as a pattern is not specific to Angular but is a general programming practice.

model knows things view shows things controller tells the view what the model knows

V  [v] <div>         [v]
    |                 |
----+-----------------+-
    |                 |
C  [c]               [c]
    |                 |
----+-----------------+-
    |                 |
M   +-------[f]-------+

Factory is a service.

Task List

  • Add project files (index.html, client.js, angular.js)
  • Create InputController
  • Create ShoppingService factory
  • Create CartController

InputController

Let's start by putting everything in one controller.

client.js

var cartApp = angular.module('cartApp', []);

cartApp.controller('InputController', ['$scope', function($scope){
  console.log('InputController called.');

  $scope.productArray = [];

  $scope.product = {
    name: '',
    cost: ''
  }

  $scope.addProduct = function(newProduct) {
    $scope.productArray.push(newProduct);
    console.log($scope.productArray)
  }
}]);

index.html

<div ng-controller="InputController">
  Product Name: <input ng-model="product.name" name="name">
  Cost: <input ng-model="product.cost" name="cost">
  <button ng-click="addProduct(product)">Add to Cart</button>

  <div ng-repeat="p in productArray">
    {{p.name}}: {{p.cost}}
  </div>
</div>

After running in the browser, you should notice two problems. First, adding a new item overrides the old one. Second, ng-repeat is throwing an error when trying to display duplicates.

  1. Angular Copy
  2. Duplicate Key

ShoppingService Factory

Now that we have our controller working, let's pull some of the functionality into a Factory.

cartApp.controller('InputController', ['$scope', 'ShoppingService', function($scope, ShoppingService){
  console.log('InputController');
  var shoppingService = ShoppingService;

  $scope.product = {
    name : '',
    cost : ''
  }

  $scope.addProduct = shoppingService.factoryAddProduct;
  $scope.productList = shoppingService.factoryProductList;
}]);

cartApp.factory('ShoppingService', [function(){
  var productList = [];

  var addProduct = function(newProduct) {
    var copy = angular.copy(newProduct);
    productList.push(copy);
  };
  return {
    factoryProductList: productList,
    factoryAddProduct: addProduct
  }
}]);

Our productList array and addProduct function have now been moved into our new ShoppingService factory. Everything should still work as expected.

CartController

Lastly, we want to move our productList display into it's own controller called CartController. The final source is available in the project files.

chi-shopping-cart's People

Contributors

christopher-black avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.