javascript - 如何在 AngularJS 中使用 ng-repeat 动态生成 ng-model ="my_{{$index}}"?

标签 javascript angularjs ng-repeat

我想问你是否可以帮我解决这个问题。

我已经为我的问题创建了一个 jsfiddle here .我需要使用 ng-model="my_{{$index}}"的方式在 ng-repeater 中使用 ng-model 动态生成一些输入。

在 jsfiddle 中,您可以看到一切正常,直到我尝试动态生成它。

html 将是:

<div ng-app>
<div ng-controller="MainCtrl">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td>
      <select ng-model="selectedQuery" 
        ng-options="q.name for q in queryList" >
        <option title="---Select Query---" value="">---Select Query---</option>
      </select>
    </td>
  </tr>
  <tr ng-repeat="param in parameters">
    <td>{{param}}:</td>
    <td><input type="text" ng-model="field_X" />field_{{$index}}</td>
  </tr>
</table>
<div>
<div>

还有 javascript...

function MainCtrl($scope) {
 $scope.queryList = [
    { name: 'Check Users', fields: [ "Name", "Id"] },
    { name: 'Audit Report', fields: [] },
    { name: 'Bounce Back Report', fields: [ "Date"] } 
  ];

$scope.$watch('selectedQuery', function (newVal, oldVal) {
    $scope.parameters = $scope.selectedQuery.fields;
  });
}

你能告诉我什么吗?

非常感谢。

最佳答案

它能解决您的问题吗?

function MainCtrl($scope) {
     $scope.queryList = [
        { name: 'Check Users', fields: [ "Name", "Id"] },
        { name: 'Audit Report', fields: [] },
        { name: 'Bounce Back Report', fields: [ "Date"] } 
      ];
    $scope.models = {};
    $scope.$watch('selectedQuery', function (newVal, oldVal) {
        if ($scope.selectedQuery) {
            $scope.parameters = $scope.selectedQuery.fields;
        }
    });
}

在你的 Controller 中:

  <tr ng-repeat="param in parameters">
    <td>{{param}}:</td>
    <td><input type="text" ng-model="models[param] " />field_{{$index}}</td>
  </tr>

Fiddle

关于javascript - 如何在 AngularJS 中使用 ng-repeat 动态生成 ng-model ="my_{{$index}}"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19573001/

相关文章:

javascript - 带有 typescript 的 Angular-js - 数据应该存储在 Controller 还是服务中?

javascript - 在 electron 的 vue-component 中打印 Canvas

javascript - 如何在 jQuery 中高效处理 jQuery(window).scroll 函数?

javascript - 提交表单或点击提交输入无效

javascript - Angular JS ng-if 不在 ng-repeat 内工作

angularjs - Angular 数据绑定(bind)不显示数据..

javascript - 我找不到一种优雅的方法将数据库源添加到 dgrid

javascript - 使用 Grunt 管理 AngularJS 模块版本和发布

javascript - 每当页面重新加载时, Angular 消息就会出现并消失

php - AngularJS 在 http 之后在 ng-repeat 中更新单个模型