2014-10-10 73 views
0

我的代碼是這樣的角應用拋出錯誤

<div ng-app='myApp' ng-controller="MainCtrl"> 
<div ng-repeat="prdElement in pacakageElement track by $index" class="package-grid"> 
    <div style="border: 1px solid; padding-bottom: 10px; padding-top: 10px"> 
     <input placeholder="Product Code" /> 
     <input placeholder="Dimension" /> 
    </div> 
    <table class="hovertable"> 
     <thead> 
      <tr> 
       <th>Line #</th> 
       <th>ITCLS</th> 
       <th>Item #</th> 
       <th>Line Quantity#</th> 
       <th>Ship Quantity</th> 
       <th>PickQuantity</th> 
       <th>Quantity in Plt</th> 
       <th>Allready Packed</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="data in prdElement.Data" ng-init="data.newquantity = data.quantity"> 
       <td>{{data.itemId}}</td> 
       <td>{{data.itcls}}</td> 
       <td>{{data.itemId}}</td> 
       <td>Line Quantity#</td> 
       <td>Ship Quantity</td> 
       <td>PickQuantity</td> 
       <td> 
        <input type="text" ng-model="data.newquantity" placeholder="Quantity" required=required /> 
       </td> 
       <td>Allready Packed</td> 
      </tr> 
      <tr> 
       <td width="100%" colspan="4"> 
        <button ng-show="prdElement.show" ng-click="newPackageItem(prdElement,$event)">Next Pallet</button> 
       </td> 
       <td width="100%" colspan="4"> 
        <button ng-show="prdElement.show">Remove Pallet</button> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

    (function() { 
    angular.module('myApp', []).controller('MainCtrl', function ($scope) { 
     var counter = 0; 
     $scope.pacakageElement = [{ 
      name: counter, 
      show: true, 

      Data: [{ 
       name: 'item 1', 
       itemId: '284307', 

       itemCode: '', 
       description: 'Bicycle parts - frame', 
       quantity: '100', 
       handlingUnit: 'CTN', 
       weight: '613.04', 
       class: '', 
       lenght: '102', 
       width: '42', 
       height: '61', 
       flag: 'P' 


      }, { 
       name: 'item 2', 
       itemId: '284308', 

       itemCode: '', 
       description: 'Bicycle parts - fork', 
       quantity: '200', 
       handlingUnit: 'CTN', 
       weight: '242.99', 
       class: '', 
       lenght: '75', 
       width: '34', 
       height: '18', 
       flag: 'P' 
      }] 
     }]; 


     $scope.newPackageItem = function (packageElement, $event) { 
      var npackageElement = {}; 
      angular.copy(packageElement, npackageElement); 
      counter++; 
      packageElement.show = false; 

      npackageElement.name = counter; 
      angular.forEach(npackageElement.Data, function (row) { 
       if (row.quantity != row.newquantity || row.quantity != 0) { 
        row.quantity = row.quantity - row.newquantity; 

       } 

      }); 

      $scope.packageElement.push(npackageElement); 
     }; 
    }); 

}()); 

在這裏,我想重複我的第一個數據集,並做一些關於它的計算。一切正常,除了功能newPackageItem。此功能單獨拋出錯誤

TypeError: Cannot read property 'push' of undefined 
at Scope.$scope.newPackageItem 

Fiddle

+0

對不起,這應該修復它'$ scope.packageElement.Data.push(npackageElement);' – Chandermani 2014-10-10 06:11:32

+0

@Chandermani沒有它的不固定。錯誤更改爲 typeError:無法讀取Scope上未定義的 的屬性'Data'。$ scope.newPackageItem – Athul 2014-10-10 06:16:40

回答

4

拼錯財產 「packageElement」 爲 「pacakageElement」。將所有實例更改爲使用「packageElement」,它應該可以工作。