javascript - 来自后端的 JSON 值的动态模型名称

标签 javascript html angularjs json angular-directive

我有一个来自后端的 Json,如下所示:

var infoArray = [
        {
            "field" : "text",
            "placeholder" : "Name",
            "modelName" : "userName"
        },{
            "field" : "date",
            "placeholder" : "DOB",
            "modelName" : "userDob" 
        },{
            "field" : "text",
            "placeholder" : "Location",
            "modelName" : "userLocation"
        }
    ]; 

It's already set that there can be at most 10 text fields and 4 date fields i can get.

由于 ModelName 来自后端,所以我尝试将其放入简单的 ng-repeat 中,但这似乎不起作用 Here is Plnkr Link

Indeed i found one solution by myself and the code seems to be bulky for that.

Here is plnkr Link

And one more thing i have to consider is that i have to use the modelName to get the values from the user and then sent it back.

有人可以建议优化的解决方案或修改我的代码吗? 谢谢!

最佳答案

你的尝试几乎是正确的。只需要删除ng-model中的大括号

 <div ng-repeat="info in infoArray">
       <input type="{{info.field}}" placeholder="{{info.placeholder}}" ng-model="info.modelName" ng-init="callFunc(info)" /> 
 </div>

Demo

关于javascript - 来自后端的 JSON 值的动态模型名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43693973/

相关文章:

javascript - HTML5/JS : Check video compatibility

javascript - 打印包含多于 1 页的表格数据时,如何在浏览器打印中添加分页符

javascript - 滚动到列表底部后,angularjs-ui-grid 有额外的空白

javascript - 捕获 Node ftp 模块中的未捕获异常错误

php - 使用ajax将数组发布到cakePHP中的 Controller

jquery - 使用 formdata 发布数组

javascript - 如何在 Angularjs 中重定向到新状态?

javascript - 使用后退按钮时如何恢复 Angular Controller 中的过滤状态?

javascript - jQuery coda slider 效果导航停止

Javascript - 模板文字 - 三元运算