angularjsangular-schema-form

empty array in angular-schema-form


I'm using angular-schema-form in my project and trying to add an empty array in my model.

I expected that array type schema form will not contain any items but it actually pushed one object in my array and showed it in from.

How can I init form with no items in array?

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


Solution

  • The value you are looking for is startEmpty: true this will avoid pre-populating the array with an object.

    Angular Schema Form: Array Documentation

    The pre-population is defaulted to ensure that the form fields within an array are available when the form loads. The startEmpty: true value can override this behaviour.