javascript - 角度模式形式的数组长度
问题描述
我有这个架构:
{
"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 项?
我已经尝试过maxItems
,minItems
但这些参数不会呈现项目。
解决方案
目前有两种方法可以做到这一点。
首先是将它们设置在默认模型中,使其看起来像:
$scope.model = {
"comments": [{},{}]
}
第二个是在数组上使用 onChange:
"onChange": function(val) { if(val.length < 2) val.push({}); }
两者之间的区别在于 onChange 不允许它低于您设置的最小长度,而第一个选项仅用于初始默认值。
推荐阅读
- javascript - 如果发生错误,如何调用两次 API?
- python - 熊猫:如何将整数出生日期转换为年龄
- lodash - Lodash 4:如果对象属性包含字符串的某个部分,如何省略它们?
- shell - 案例:C shell 中 switch 参数过多错误
- r - 从 R 脚本循环 tbl_regression 不为 rmarkdown::render 打印
- javascript - Javascript-Resolve 对 HTML 的承诺
- node.js - 节点检查器离线?
- wordpress - 如何更改折线图和条形图(highcharts / wordpress)的颜色?
- logging - 如何在 AWS Glue 工作人员(内部地图功能)中记录消息?
- php - 将收银员模型从用户更改为酒店