angular-schema-form 中的空数组

empty array in angular-schema-form

我在我的项目中使用 angular-schema-form 并试图在我的模型中添加一个空数组。

我预计数组类型模式表单不会包含任何项目,但它实际上将一个对象推送到我的数组中并将其显示在 from 中。

如何在数组中没有项目的情况下初始化表单?

html

<div ng-app="app" ng-controller="Controller as ctr">
  <form sf-schema="ctr.schema" sf-form="ctr.form" sf-model="ctr.model"></form>
  {{ctr.model.comments}} - Where this object come from? This array was empty. Is it possible to keep it empty on load?
</div>

js

var myApp = angular.module('app', ['schemaForm'])
  .controller("Controller", function() {
    this.schema = {
      "type": "object",
      "title": "Comment",
      "properties": {
        "comments": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "title": "Name",
                "type": "string"
              },
              "comment": {
                "title": "Comment",
                "type": "string",
                "maxLength": 20,
                "validationMessage": "Don't be greedy!"
              }
            }
          }
        }
      }
    };

    this.form = [{
      "key": "comments",
      "items": [
        "comments[]"
      ]
    }];

    this.model = {
      comments: [] // Empty array defined
    };
  });

Jsfiddle

您要查找的值是 startEmpty: true 这将避免用对象预填充数组。

Angular Schema Form: Array Documentation

默认预填充以确保数组中的表单字段在加载表单时可用。 startEmpty: true 值可以覆盖此行为。