类型错误:无法读取 ng-repeat 的未定义属性

TypeError: Cannot read property of undefined for ng-repeat

本文关键字:ng-repeat 未定义 属性 读取 错误 类型      更新时间:2023-09-26

我正在尝试让ng-repeat在收据中重复项目列表。我有这个工作的编辑功能,但我无法让添加页面工作,因为它显示:

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>

提前非常感谢您的帮助!

您需要先定义范围的属性"模型",然后才能为其分配"spiff"。您可以通过两种方式执行此操作:

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

你正在写$scope.model.spiffs。这里的模型是未定义的,因此存在错误。

类型错误:无法读取未定义的属性"spiffs"

你需要先初始化$scope.model,像这样

$scope.模型 = {}

现在,由于$scope.model 不是未定义的,因此您可以添加一个属性。

希望对您有所帮助。