angular-schema-form数组长度

angular-schema-form array length

我有这个架构:

    {
     "type": "object",
     "title": "Comment",
     "required": [
     "comments"
      ],
  "properties": {
    "comments": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "title": "Name",
            "type": "string"
          },
          "email": {
            "title": "Email",
            "type": "string",
            "pattern": "^\S+@\S+$",
            "description": "Email will be used for evil."
          },
          "spam": {
            "title": "Spam",
            "type": "boolean",
            "default": true
          },
          "comment": {
            "title": "Comment",
            "type": "string",
            "maxLength": 20,
            "validationMessage": "Don't be greedy!"
          }
        },
        "required": [
          "name",
          "comment"
        ]
      }
    }
  }
}

这是一组评论。我可以添加和删除评论。

如何在默认情况下始终渲染数组的 2 项?

我已经尝试使用 maxItemsminItems,但这些参数不会呈现项目。

目前有两种方法。

首先是将它们设置为默认模型,如下所示:

$scope.model = {
    "comments": [{},{}]
}

第二种是在数组上使用 onChange:

"onChange": function(val) { if(val.length < 2) val.push({}); }

两者的区别在于 onChange 不允许它低于您设置的最小长度,而第一个选项仅用于初始默认值。