简体   繁体   English

Ang -J中的AngularJS动态数据绑定

[英]AngularJS dynamic data binding in ng-repeat

I want to call an attribute of an data-binded object dynamically based on the ng-repeat object. 我想基于ng-repeat对象动态调用数据绑定对象的属性。 I have created a simple setup, can anybody solve this, if it is solvable like this? 我已经创建了一个简单的设置,任何人都可以解决这个问题,如果它可以像这样解决吗?

The input should get the value of the "person.item". 输入应该获得“person.item”的值。 For example: person.id -> 100 例如:person.id - > 100

http://jsfiddle.net/q7gs3njj/ http://jsfiddle.net/q7gs3njj/

html HTML

<div ng-app ng-controller="TestController">
    <div ng-repeat="item in list">
        <label>{{ item }}:</label>
        <input  type="text"/>
    </div>
    {{list}}
</div>

javascript JavaScript的

function TestController($scope) {
    $scope.list = [ 'id', 'name', 'gender' ];

    $person = { id:'100', name:'John', age:'22', gender:'Male' };

}

Thank you! 谢谢!

Of course, just use item as index: 当然,只需使用item作为索引:

<div ng-app ng-controller="TestController">
    <div ng-repeat="item in list">
        <label>{{ item }}:</label>
        <input  type="text" ng-model="person[item]"/>
    </div>
    {{list}}
</div>

And the person must be in the scope: 这个人必须在范围内:

function TestController($scope) {
    $scope.list = [ 'id', 'name', 'gender' ];
    $scope.person = { id:'100', name:'John', age:'22', gender:'Male' };   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM