2016-09-07 44 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值可以覆蓋此行爲。