如何动态生成ng-model="my_{{$index}}";在 AngularJS 中使用 ng-repeat? [英] How to generates dynamically ng-model="my_{{$index}}" with ng-repeat in AngularJS?
本文介绍了如何动态生成ng-model="my_{{$index}}";在 AngularJS 中使用 ng-repeat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问你是否可以帮我解决这个问题.
我用我的问题这里创建了一个jsfiddle.我需要使用 ng-model="my_{{$index}}" 的方式在 ng-repeater 中使用 ng-model 动态生成一些输入.
在 jsfiddle 中,您可以看到一切正常,直到我尝试动态生成它.
html 将是:
<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="---选择查询---" value="">---选择查询---</option></选择></td></tr><tr ng-repeat="参数中的参数"><td>{{param}}:</td><td><input type="text" ng-model="field_X"/>field_{{$index}}</td></tr><div><div>还有 javascript...
function MainCtrl($scope) {$scope.queryList = [{ name: 'Check Users', fields: [ "Name", "Id"] },{名称:'审计报告',字段:[]},{ 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: 'Bounce Back Report', fields: [ "Date"] }];$scope.models = {};$scope.$watch('selectedQuery', function (newVal, oldVal) {如果($scope.selectedQuery){$scope.parameters = $scope.selectedQuery.fields;}});}
在你的控制器中:
<td>{{param}}:</td><td><input type="text" ng-model="models[param] "/>field_{{$index}}</td></tr>I would like to ask you if you can give me a hand on this.
I have created a jsfiddle with my problem here. I need to generate dynamically some inputs with ng-model in a ng-repeater using the way ng-model="my_{{$index}}".
In jsfiddle you can see that everything it's working fine until I try to generate it dynamically.
The html would be:
<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>
And the 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;
});
}
Can you give me any idea?
Thanks a lot.
解决方案 Does it solve your problem?
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;
}
});
}
And in your controller:
<tr ng-repeat="param in parameters">
<td>{{param}}:</td>
<td><input type="text" ng-model="models[param] " />field_{{$index}}</td>
</tr>
这篇关于如何动态生成ng-model="my_{{$index}}";在 AngularJS 中使用 ng-repeat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆