1

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

1
  • But if I what to init the array with 2 empty objecs? Commented May 31, 2018 at 0:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But if I what to init the array with 2 empty objecs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.