2016-02-29 34 views
0

我試圖讓ng-repeat在收據中重複項目列表,spiffs。我有這個工作的編輯功能,但我不能得到添加頁面的工作,因爲它讀取:TypeError:無法讀取ng-repeat的undefined屬性

TypeError: Cannot read property 'spiffs' of undefined 

這裏是代碼導致它:

$scope.model.spiffs = []; 
$scope.model.spiffs = [{ spiff_id: 0, receipt_id: 0, product_id: 0, spiff_amt:0, quantity: 1, active: true, note: null }]; 

起初是第二但我想我會試着讓$ scope.model是一個空數組來嘗試提前聲明數組,我嘗試了不同的變體,我想我只是缺少一些簡單的東西。當我沒有任何初始輸入來填充表單時,我需要做什麼才能將表單設置爲ng - 重複一組字段?之後,我可以通過簡單的$scope.model調用獲取所有表單數據並將其發送到我的API?

這裏是希望將要實現HTML:

<div class="col-xs-10 col-xs-offset-2" ng-repeat="item in model.spiffs"> 

    <div ng-hide="item.active === false"> 
    <div class="col-xs-4 form-group"> 
     <select name="productID" ng-model="item.product_id" required="" ng-change="changeSpiffProduct($index)" class="form-control" 
     ng-options="product.product_id as product.model_number + ' ' + product.name for product in products"> 
     </select> 
    </div> 


    <div class="col-xs-2 form-group"> 
      <input name="spiff_sale_price" type="text" placeholder="" ng-model="item.quantity" class="form-control input-md"> 
    </div> 

    <div class="col-xs-2 form-group"> 
      <p name="spiff_amt" class="form-control-static">{{item.spiff_amt | currency: "$"}}</p> 
    </div> 

    <div class="col-xs-2 form-group"> 
      <p name="spiff_total" class="form-control-static">{{item.spiff_amt * item.quantity | currency: "$"}}</p> 
    </div> 

    <div class="col-xs-2 form-group"> 
     <button class="btn btn-danger" novalidate ng-click="removeSpiff($index)"><i class="fa fa-minus"></i></button> 
    </div> 

    </div> 

</div> 

非常感謝您預先的幫助!

+0

我們需要看看你的控制器和'$ scope.model.spiffs'來自哪裏,給你適當的建議。 – martin

+0

它假設顯示TypeError:無法設置未定義屬性'spiffs' – Siva

回答

4

您需要首先定義範圍的屬性「模型」,然後才能爲其指定「spiff」。你可以這樣做有兩種方式:

$scope.model = {}; 
 

 
//OR: 
 

 
$scope.model = { 
 
    spiffs: [] 
 
};

1

你正在寫$ scope.model.spiffs。這裏模型是未定義的,因此是錯誤的。

TypeError: Cannot read property 'spiffs' of undefined

您需要先初始化$ scope.model,這樣

$scope.model = {}

現在既然$ scope.model不是不確定的,你可以添加屬性。

希望它有幫助。

相關問題