2016-09-07 73 views
1

我在我的项目中使用角度模式形式,并试图在我的模型中添加一个空数组。角度模式形式的空数组

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

我怎样才能初始化形式没有数组中的项目?

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

你正在寻找的值是startEmpty: true这将避免预填充与对象的数组。

Angular Schema Form: Array Documentation

预人口默认为确保一个阵列内的表单字段是当加载窗体可用。 startEmpty: true值可以覆盖此行为。