使用angularjs NG重复内NG-Binding模型 [英] Binding using ng-model inside ng-repeat in angularjs

查看:172
本文介绍了使用angularjs NG重复内NG-Binding模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合的典范用户输入字段列表。我不知道事先领域,所以我写一个通用的code设置基于字段的形式。

I'm trying to bind a model 'User' to a list of input fields. I do not know the fields beforehand, so i've to write a generic code to setup the form based on the fields.

<script>
    function MyController($scope){
        $scope.fields  = ['name','password','dob'];
        $scope.user1 = {name:"Shahal",password:"secret"}
    };
</script>
<div ng-app ng-controller="MyController">
    <ul>
        <li ng-repeat="field in fields">
            <label>{{field}}</label><input type="text" ng-model="user1.{{field}}">
        </li>
    </ul>
    <pre>{{fields}}</pre>
</div>

我穿过田野试图循环,并显示每个字段(范围提供)输入字段。但结合是不妥当的,因为我正在努力评估里面一个前pression NG-模式。

I'm trying to loop through the fields and show a input field for each fields (available in scope). But the binding is not proper as i'm trying to evaluate an expression inside ng-model.

基本上我试图表现出3输入字段(名称,密码,DOB)与连接到对应的字段对象为user1。

Basically i'm trying to show 3 input fields (name,password,dob) with the object user1 attached to the corresponding field.

这里的小提琴

任何帮助吗?

推荐答案

下面将解决您的问题。

<script>
    function MyController($scope){
        $scope.fields  = ['name','password','dob'];
        $scope.user1 = {name:"Shahal",password:"secret"}
    };
</script>
<div ng-app ng-controller="MyController">
    <ul>
        <li ng-repeat="field in fields">
            <label>{{field}}</label><input type="text" ng-model="user1[field]">
        </li>
    </ul>
    <pre>{{fields}}</pre>
</div>

这篇关于使用angularjs NG重复内NG-Binding模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆