gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:06:54 24 4
gpt4 key购买 nike

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

我已经为我的问题创建了一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com