is data a reserved keyword in angularjs?

Issue

The code below doesn’t work if I use data in lst, it works, if I replace data with x, so is data a reserved keyword in AngularJS and why?

<script src="http://ajadata.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app='app1'>
  <div id='Grocery' ng-controller='Grocery'>
    <h3>Grocery List</h3>
    <div ng-repeat='data in lst'>
      <h4>{{data.item}}</h4>
    </div>
    <br>

    <p> enter item:
      <input type="text" ng-model='addItem' />
    </p>
    <button ng-click='addTolist(addItem)'>Add to list</button>
    <button ng-click='addTolist(addItem)'>Add to list</button>
    <h2>{{NoItemError}}</h2>
  </div>
  <!-- End of Grocery -->
  <script>
    var app1 = angular.module('app1', []);
    app1.controller('Grocery', function($scope) {
      $scope.lst = [{
          item: 'banana',
          needed: false
        },
        {
          item: 'apple',
          needed: false
        },
        {
          item: 'milk',
          needed: false
        },
        {
          item: 'tomato',
          needed: false
        },
        {
          item: 'juice',
          needed: false
        }
      ]
      $scope.addTolist = function(addItem) {
        if (!(addItem === undefined || addItem === '')) {
          $scope.lst.push({
            item: addItem,
            needed: false
          });
          $scope.NoItemError = '';
        } else {
          $scope.NoItemError = 'Please enter an item';
        }
      }
    });
  </script>
</body>

enter image description here

Solution

While replacing text with another, you broke your CDN URL for the AngularJS script.
It became http://ajadata.googleapis.com

Change it to http://ajax.googleapis.com

Here is a full script:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

And here is a working example of your code:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app='app1'>
  <div id='Grocery' ng-controller='Grocery'>
    <h3>Grocery List</h3>
    <div ng-repeat='data in lst'>
      <h4>{{data.item}}</h4>
    </div>
    <br>

    <p> enter item:
      <input type="text" ng-model='addItem' />
    </p>
    <button ng-click='addTolist(addItem)'>Add to list</button>
    <button ng-click='addTolist(addItem)'>Add to list</button>
    <h2>{{NoItemError}}</h2>
  </div>
  <!-- End of Grocery -->
  <script>
    var app1 = angular.module('app1', []);
    app1.controller('Grocery', function($scope) {
      $scope.lst = [{
          item: 'banana',
          needed: false
        },
        {
          item: 'apple',
          needed: false
        },
        {
          item: 'milk',
          needed: false
        },
        {
          item: 'tomato',
          needed: false
        },
        {
          item: 'juice',
          needed: false
        }
      ]
      $scope.addTolist = function(addItem) {
        if (!(addItem === undefined || addItem === '')) {
          $scope.lst.push({
            item: addItem,
            needed: false
          });
          $scope.NoItemError = '';
        } else {
          $scope.NoItemError = 'Please enter an item';
        }
      }
    });
  </script>
</body>

Most reserved words in AngularJS start with $, for example $index or $id, etc.

Answered By – Aleksey Solovey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published