我的程序正在运行,但是最后会出现null值。如何在我的程序中删除此错误。为什么它会到来?

时间:2016-09-20 05:53:08

标签: angularjs angularjs-directive angularjs-ng-repeat

int

3 个答案:

答案 0 :(得分:0)

纠正了一些错误。请查看此工作plunker

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

myapp.controller('productController',function($scope){ 

$scope.listProducts=[ {id :'P01',name :'Milk',price : 40,quantity:10 }, {id :'P02',name :'Butter',price :50,quantity:11 }, {id :'P03',name :'Biscuits',price :60,quantity:12 } ,{id :'P04',name :'Chocolate',price :70,quantity:13 } ];

 });

答案 1 :(得分:0)

您的应用程序中存在很多语法错误。其次,你的角度参考是错误的。请查看以下关于plunker的工作示例:

<强>的index.html

<!DOCTYPE html>
<html lang="en-US" ng-app="myApp">

  <head>
    <meta charset="ISO-8859-1" />
    <title>AngularJS_Index</title>
 </head>

  <body>
    <script src="https://code.angularjs.org/1.4.8/angular.js"></script>
    <script type="text/javascript" src="script.js"></script>
    <div ng-controller="productController">
      <table border="2">
      <tbody>
        <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Price</th>
          <th>Quantity</th>
        </tr>
        <tr data-ng-repeat="product in listProducts">
          <td>{{product.id}}</td>
          <td>{{product.name}}</td>
          <td>{{product.price}}</td>
          <td>{{product.quantity}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>

<强>的script.js

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

myapp.controller('productController', function($scope) {

  $scope.listProducts = [{
    id: 'P01',
    name: 'Milk',
    price: 40,
    quantity: 10
  }, {
    id: 'P02',
    name: 'Butter',
    price: 50,
    quantity: 11
  }, {
    id: 'P03',
    name: 'Biscuits',
    price: 60,
    quantity: 12
  }, {
    id: 'P04',
    name: 'Chocolate',
    price: 70,
    quantity: 13
  }];

});

LINK

答案 2 :(得分:0)

,运算符分隔每个对象。您的解决方案将完美运行:

尝试:

$scope.listProducts=[ {id :'P01',name :'Milk',price : 40,quantity:10 }, {id :'P02',name :'Butter',price :50,quantity:11 }, {id :'P03',name :'Biscuits',price :60,quantity:12 }, {id :'P04',name :'Chocolate',price :70,quantity:13 } ];

代替:

$scope.listProducts=[ {id :'P01',name :'Milk',price : 40,quantity:10 } {id :'P02',name :'Butter',price :50,quantity:11 } {id :'P03',name :'Biscuits',price :60,quantity:12 } {id :'P04',name :'Chocolate',price :70,quantity:13 } ];

输出

answer

工作小提琴: enter image description here

感谢。