简体   繁体   中英

how to get the value of ng-repeat input angularjs?

i created some dynamic input from ng-repeat.

i just want to know, how to get the input value of each dynamically generated input field in 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);
});

Bind your input text to the author object's key'd property works.

<input type="text" ng-model="author[key]">

Note the following as suggested by ebinmanuval does NOT work; this is because it fails to mutate the underlying author object and instead just overwrites the value property. The trick here is that we want to bind the model to a reference type like an object not a value type like a string, that way when we mutate the model the underlying type is mutated as well:

<input type="text" ng-model="value">

https://jsfiddle.net/775dfa9y/4/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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