数据是angularjs中的保留关键字吗?

时间:2018-07-19 09:15:12

标签: angularjs

如果我使用data in lst,下面的代码不起作用,如果我用data 替换 x,它会起作用,所以data是保留关键字在AngularJS中,为什么?

<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

1 个答案:

答案 0 :(得分:1)

在用另一个文本替换文本时,您破坏了AngularJS脚本的CDN URL。
它变成了http://ajadata.googleapis.com

将其更改为http://ajax.googleapis.com

以下是完整的脚本:

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

这是您的代码的有效示例:

<script src="http://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>


AngularJS中大多数保留字都以$开头,例如$index$id等。