angular-schema-form数组长度

时间:2018-05-30 16:53:01

标签: javascript angularjs angular-schema-form

我有这个架构:

    {
     "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,但这些参数不会呈现项目。

1 个答案:

答案 0 :(得分:0)

目前有两种方法。

首先是将它们设置为默认模型,因此其外观类似于:

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

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

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

两者之间的区别是onChange不允许它降至您设置的最小长度以下,而第一个选项仅用于初始默认值。