2014-10-08 93 views
0

我的代碼是這樣的AngularJS拋出未知錯誤

<body> 
<div> 
    <table ng-app='myApp'> 
     <thead> 

     </thead> 
     <tbody ng-controller="MainCtrl"> 
      <tr ng-repeat="prdElement in palletElement"> 

       <td> 
        {{prdElement.name}} 
       </td> 

       <td> 
        {{prdElement.itemId}} 
       </td> 
       <td> 
        {{prdElement.shipmentId}} 
       </td> 
       <td> 
        {{prdElement.itemCode}} 
       <td> 
        {{prdElement.description}} 
       </td> 

       <td> 
        {{prdElement.handlingUnit}} 
       </td> 
       <td> 
        {{prdElement.weight}} 
       </td> 
       <td> 
        {{prdElement.class}} 
       </td> 
       <td> 
        {{prdElement.lenght}} 
       </td> 

       <td> 
        {{prdElement.width}} 
       </td> 
       <td> 
        {{prdElement.height}} 
       </td> 
       <td> 
        {{prdElement.flag}} 
       </td> 

       <td> 
        <input type="text" ng-model="prdElement.quantity" placeholder=" Code" required /> 

       </td> 
       <td> 

        <button ng-click="newPrdItem(prdElement,$event)">Submit</button> 

       </td> 
      </tr> 
      <tr> 

      </tr> 
     </tbody> 

    </table> 
</div> 

(function() { 
    angular.module('myApp', []).controller('MainCtrl', function ($scope) { 

     var counter = 0; 

     $scope.palletElement = [{ 
      name: 'Pallet 1', 
      itemId: '284307', 
      shipmentId: 'eb44f690-c97a-40e3-be2a-0449559e171a', 
      itemCode: '', 
      description: 'Bicycle parts - frame', 
      quantity: '31', 
      handlingUnit: 'CTN', 
      weight: '613.04', 
      class:'', 
      lenght: '102', 
      width: '42', 
      height: '61', 
      flag:'P' 

     }, { 
      name: 'Pallet 2', 
      itemId: '284308', 
      shipmentId: 'eb44f690-c97a-40e3-be2a-0449559e171a', 
      itemCode: '', 
      description: 'Bicycle parts - fork', 
      quantity: '22', 
      handlingUnit: 'CTN', 
      weight: '242.99', 
      class: '', 
      lenght: '75', 
      width: '34', 
      height: '18', 
      flag: 'P' 
     }] 

     $scope.newPrdItem = function (prdElement, $event) { 
      counter++; 
      alert(prdElement.name); 
      prdElement.push({ 
       name: prdElement.name, 
       itemId: prdElement.itemId, 
       shipmentId: prdElement.shipmentId, 
       itemCode: prdElement.itemCode, 
       description: prdElement.description, 
       quantity: prdElement.quantity, 
       handlingUnit: prdElement.handlingUnit, 
       weight: prdElement.weight -10, 
       class: prdElement.class, 
       lenght: prdElement.lenght, 
       width: prdElement.width, 
       height: prdElement.height, 
       flag: prdElement.flag, 
      }); 

     } 

    }); 
}()); 

一切看起來還好我,但是這將引發在按鈕未知錯誤點擊

TypeError: undefined is not a function at Scope.$scope.newPrdItem

Fiddle

回答

1

您通過發佈push它的呼叫處理prdElement作爲數組,但實際上是從視圖中傳遞的對象:

這條線是什麼失敗。

prdElement.push({ 

我會創建一個單獨的陣列跟蹤的項目被點擊

var items = []; 
$scope.newPrdItem = function (prdElement, $event) { 
      counter++; 
      alert(prdElement.name); 
      items.push({ 
       name: prdElement.name, 
       itemId: prdElement.itemId, 
       shipmentId: prdElement.shipmentId, 
       itemCode: prdElement.itemCode, 
       description: prdElement.description, 
       quantity: prdElement.quantity, 
       handlingUnit: prdElement.handlingUnit, 
       weight: prdElement.weight -10, 
       class: prdElement.class, 
       lenght: prdElement.lenght, 
       width: prdElement.width, 
       height: prdElement.height, 
       flag: prdElement.flag, 
      }); 

     } 
0

你可以只有.push()到一個數組。但是您傳遞的參數prdElement是一個對象,而不是一個數組,因此prdElement.push()不是一個已定義的函數!

您最想推入的是palletElement。