通过json动态绑定作用域到ng模型

Binding scope to ng-model dynamically through json

本文关键字:ng 模型 作用域 json 动态绑定 通过      更新时间:2024-01-22

我正在尝试通过json自动填充表单,如下所示。我希望ng模型绑定到formValue.question001的范围。怎么能做到呢?

$scope.formValue = [
    {
        _id: '001',
        text: 'text-001',
    },
    {
        _id: '002',
        text: 'text-002'
    }
]
<form name="form" ng-submit="submitForm(formValue)">
    <div ng-repeat="item in formValue">
        <input type="text" ng-model="formValue.question{{ item._id }}">
    </div>
</form>

1)formValue对象中没有属性"question#"。2) 没有办法对求值表达式进行插值,您可以给ng模型一个表达式,它不是一个简单的字符串,所以您可以执行类似于ng model="questions[item_id]"的操作,其中questions是位于您的作用域中的数组