0

I am new to Angular JS. I am writing a code where I need to count items that are comma-separated. However, in my case it counts the commas as well. For eg: if I type 1,2,3 in a text box it should return 3 in my case it returns 5. Please help below is my code.

<!DOCTYPE html>
<html lang="en" ng-app="NameCalculator">
  <head>
    <title>Lunch Checker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="styles/bootstrap.min.css" />
    <style>
      .message {
        font-size: 1.3em;
        font-weight: bold;
      }
    </style>
    <script src="angular.min.js"></script>
    <script src="app.js"></script>
  </head>
  <body>
    <div class="container" ng-controller="NameCalculatorController">
      <h1>Lunch Checker</h1>

      <div class="form-group">
        <input
          id="lunch-menu"
          type="text"
          placeholder="list comma separated dishes you usually have for lunch"
          class="form-control"
          ng-model="name"
          ng-keyup="displayNumeric();"
        />
      </div>
      <div>Total Numeric value of person's name is: {{totalValue}}</div>
      <div class="form-group">
        <button class="btn btn-default" ng-click="feedYaakov()">
          Check If Too Much
        </button>
      </div>
      <div class="form-group message">
        <!-- Your message can go here. -->
      </div>
    </div>
  </body>
</html>
///And here is the JS////
(function() {
  "use strict";

  angular
    .module("NameCalculator", [])

    .controller("NameCalculatorController", function($scope) {
      $scope.name = [];
      $scope.length = "";

      $scope.displayNumeric = function() {
        $scope.totalValue = $scope.name.length;
      };
    });
})();
Raghul SK
  • 1,256
  • 5
  • 22
  • 30

0 Answers0