1

我在我的项目中使用 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
    };
  });

提琴手

4

1 回答 1

1

您正在寻找的值是startEmpty: true这将避免使用对象预先填充数组。

Angular Schema 表单:数组文档

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

于 2017-06-06T04:46:08.150 回答