【问题标题】:angular-schema-form array length角度模式形式的数组长度
【发布时间】:2018-05-30 16:53:01
【问题描述】:

我有这个架构:

    {
     "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"
        ]
      }
    }
  }
}

这是一个 cmets 数组。我可以添加和删除 cmets。

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

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

【问题讨论】:

    标签: javascript angularjs angular-schema-form


    【解决方案1】:

    目前有两种方法。

    首先是将它们设置为默认模型,使其看起来像:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-25
      • 2019-03-03
      • 2020-09-02
      • 2018-07-31
      • 2017-06-17
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多