【发布时间】:2018-05-21 09:47:05
【问题描述】:
我正在构建一个包含测试题的考试页面。这些问题来自“填补空白”类型。我从ajax请求中得到了问题。这些问题是带有特定占位符的简单文本,用于“填补空白”的位置。这些问题可以有一个或多个“填补空白”。
这是一个例子:
The apples are {green} or {red}, some times {yellow}.
这应该转换为<p>The apples are <input type='text' v-model='gap1'> or <input type='text' v-model='gap2'>, some times <input type='text' v-model='gap3'>。
到目前为止,我已经设法在我的组件中使用它们来计算值:
computed: {
addGapsField () {
let reg = /\{.*?\}/g
let mtch = ''
let text = this.question
// loop through each match
while((mtch = reg.exec(text)) !== null) {
text = text.replace(mtch[0],"<input type='text'>")
}
return text
}
}
如何将 v-model 绑定到此动态生成的输入字段。或者有没有其他方法(v-model 不是我正在寻找的必要解决方案,我不太熟悉 Vue.js 的其他选项)。
【问题讨论】:
-
如果你想创建你的模板动态,你需要使用渲染函数来做到这一点,见vuejs.org/v2/guide/render-function.html
-
我认为这是一个有趣的问题。这是我刚刚玩弄它的一些代码。 codepen.io/Kradek/pen/VxgOjg?editors=1010
标签: javascript vue.js