我从ng-repeat创建了一些动态输入。
我只想知道,如何在HTML中获取每个动态生成的输入字段的输入值。https://jsfiddle.net/a6at8js6/5/
.html
<div ng-app="mainApp" ng-controller="mainCtrl">
<div ng-repeat="author in author_group" class="authorAndTagGroup">
<div ng-repeat="(key, value) in author">
<label class="col-sm-3 control-label">{{key}}</label>
<input type="text" value="{{value}}">
</div>
</div>
</div>
Angularjs
var app = angular.module("mainApp", []);
app.controller('mainCtrl', function($scope){
$scope.authors = [{'author_name': 'akshay', 'about_author':'this is about author', 'author_image':'image url here'}];
$scope.author_group = [];
for (var key in $scope.authors) {
$scope.author_group.push({'Author Name' : $scope.authors[key].author_name, 'About the Author' : $scope.authors[key].about_author, 'Author Image' : $scope.authors[key].author_image, 'Author Linkedin Profile' : '', 'Author Twitter Profile' : ''});
}
console.log($scope.author_group);
});
将输入文本绑定到作者对象的 key'd 属性工作。
<input type="text" ng-model="author[key]">
请注意,ebinmanuval 建议的以下内容不起作用;这是因为它无法改变底层作者对象,而只是覆盖 value 属性。这里的诀窍是,我们希望将模型绑定到像对象这样的引用类型,而不是像字符串这样的值类型,这样当我们改变模型时,底层类型也会发生变化:
<input type="text" ng-model="value">
https://jsfiddle.net/775dfa9y/4/