javascript - 如何获取ng-repeat输入angularjs的值?

标签 javascript html angularjs

我从 ng-repeat 创建了一些动态输入。

我只是想知道,如何在 html 中获取每个动态生成的输入字段的 input 值。 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>

Angular

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)到 author 对象的 key'd 属性有效。

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

请注意 ebinmanuval 建议的以下内容 NOT 有效;这是因为它无法改变底层的 author 对象,而只是覆盖了 value 属性。这里的技巧是我们想将模型绑定(bind)到一个引用类型,比如一个对象,而不是一个值类型,比如一个字符串,这样当我们改变模型底层类型时也发生了变异:

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

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

关于javascript - 如何获取ng-repeat输入angularjs的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36880279/

相关文章:

javascript - 如何使用 HTML5 重命名下载的文件?

javascript - 使用 redux-thunk 会产生未定义的错误,而 Promise 工作得很好

html - 导入特定 Material 图标

angularjs - Angular ng-bind-html 和其中的指令

javascript - 谷歌分析页面跟踪在 angularjs 应用程序中无法正常工作

javascript - 如何使用 useEffect Hook 将 DOM 元素添加到我的状态,而不会在控制台中出现错误/警告?

javascript - 删除最后一个元素后的 "comma"分隔符

Javascript 运行日期时间返回值在 Chrome 中有效,但在 Internet Explorer 中无效

html && CSS - 下拉菜单不出现

html - 不需要结束标记的自定义 Angular 指令?