Javascript Angular JS:使用动态 ng-model 进行 ng-repeat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25680264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular JS: ng-repeat with dynamic ng-model
提问by MDT
I have this workingpiece of code that is repeated multiple times, hence would be great for a ng-repeat loop. For example, two instances of my code are the following.
我有这段重复多次的工作代码,因此非常适合 ng-repeat 循环。例如,我的代码的两个实例如下。
<div>
<input type="text" ng-model="searchParamaters.userName" placeholder="User Name"/>
<i class="fa fa-times" ng-click="resetSearchField(filterParamDisplay[0].param)" ng-show="showParam(filterParamDisplay[0].param)"></i>
</div>
<div>
<input type="text" ng-model="searchParamaters.userEmail" placeholder="User Email"/>
<i class="fa fa-times" ng-click="resetSearchField(filterParamDisplay[1].param)" ng-show="showParam(filterParamDisplay[1].param)"></i>
</div>
This is the filterParamDisplay array in Javascript:
这是 Javascript 中的 filterParamDisplay 数组:
$scope.filterParamDisplay = [
{param: 'userName', displayName: 'User Name'},
{param: 'userEmail', displayName: 'User Email'}
];
I have been trying to do that into a ng-repeat loop, but without success so far. This is what I have coded atm.
我一直试图在 ng-repeat 循环中做到这一点,但到目前为止没有成功。这是我编码的atm。
<div ng-repeat="param in filterParamDisplay">
<input type="text" ng-model="searchParams[{{param}}]" placeholder="{{param.displayName}}"/>
<i class="fa fa-times" ng-click="resetSearchField(filterParamDisplay[$index].param)" ng-show="showParam(filterParamDisplay[$index].param)"></i>
</div>
The problems are into the ng-model variable above, and into the $index in the ng-click and ng-show. Not sure if this can be done at all, any help is much appreciated, thanks!
问题出在上面的 ng-model 变量中,以及 ng-click 和 ng-show 中的 $index 中。不确定这是否可以完成,非常感谢任何帮助,谢谢!
UPDATE: Thanks for all the answers, using
更新:感谢所有的答案,使用
<div ng-repeat="p in filterParamDisplay">
...
ng-model="searchParams[p]"
Works great!
效果很好!
Still struggling on the showParam and resetSearchField functions which do not work properly yet using $index. Here is my code.
仍然在 showParam 和 resetSearchField 函数上苦苦挣扎,但使用 $index 仍无法正常工作。这是我的代码。
$scope.searchParams = $state.current.data.defaultSearchParams;
$scope.resetSearchField = function (searchParam) {
$scope.searchParams[searchParam] = '';
};
$scope.showParam = function (param) {
return angular.isDefined($scope.searchParams[param]);
};
回答by Cem ?zer
As you bind your ng-models to searchParameters.userNameand searchParameters.userMailat first example, you must use searchParameters[param.param]for ng-model in ng-repeat. Also like others said, you don't need to use $index, you got your object as paramin ng-repeat scope.
当你绑定你的NG-模型searchParameters.userName,并searchParameters.userMail在第一个例子中,你必须使用searchParameters[param.param]在NG-重复了NG-模型。也像其他人说的那样,你不需要使用 $index,你的对象就像param在 ng-repeat 范围内一样。
<div ng-repeat="param in filterParamDisplay">
<input type="text" ng-model="searchParameters[param.param]" placeholder="{{param.displayName}}"/>
<i class="fa fa-times" ng-click="resetSearchField(param.param)" ng-show="showParam(param.param)"></i>
</div>
Here is working FIDDLE
这是工作中的FIDDLE
回答by Rajamohan Anguchamy
<div ng-app="dynamicAPP">
<div ng-controller="dynamicController">
<div ng-repeat="param in filterParamDisplay">
<input type="text" ng-model="searchParams[param.param]" placeholder="{{param.displayName}}" /> <i class="fa fa-times" ng-click="resetSearchField(filterParamDisplay[$index].param)" ng-show="showParam(filterParamDisplay[$index].param)"></i>
</div>{{searchParams}}</div>
</div>
Jsfiddler link this one for get a single object like 'username' or 'email'
Jsfiddler 链接这个以获得单个对象,如“用户名”或“电子邮件”
you want single value in ng-show and ng-click use above one. or other wise use belowed one.
你想在 ng-show 和 ng-click 中使用单个值高于一个值。或以其他方式明智地使用下面的一个。
<div ng-app="dynamicAPP">
<div ng-controller="dynamicController">
<div ng-repeat="param in filterParamDisplay">
<input type="text" ng-model="searchParams[param.param]" placeholder="{{param.displayName}}" /> <i class="fa fa-times" ng-click="resetSearchField(.param)" ng-show="showParam(param)"></i>
</div>{{searchParams}}</div>
</div>
Jsfiddler link this one is get whole object based on the control
this will passes the whole set of object list.
这将传递整个对象列表集。
回答by Foo L
You don't need to interpolate angular variables inside ng-*directives.
您不需要在ng-*指令中插入角度变量。
Try:
尝试:
HTML:
HTML:
<div ng-repeat="p in filterParamDisplay">
<input type="text" ng-model="searchParams[p]" placeholder="{{p.displayName}}"/>
<i ng-click="printme(p.param)">click</i>
</div>
Controller:
控制器:
$scope.filterParamDisplay = [
{param: 'userName', displayName: 'User Name'},
{param: 'userEmail', displayName: 'User Email'}
];
$scope.printme = function(v) {
console.log(v);
};
回答by MiTa
As @aarosil said you do not need to use $index.
I wrote a small jsfiddle, I don't know your logic behind showParam so I mocked it.
正如@aarosil 所说,您不需要使用$index. 我写了一个小的 jsfiddle,我不知道你在 showParam 背后的逻辑,所以我嘲笑它。
View :
看法 :
<div ng-controller="Ctrl">
<div ng-repeat="param in filterParamDisplay">
<input type="text" ng-model="searchParams[param.param]" placeholder="{{param.displayName}}"/>
<i class="fa fa-times" ng-click="resetSearchField(param)" ng-show="showParam(param)"></i>
</div>
</div>
and controller :
和控制器:
$scope.searchParams = {};
$scope.filterParamDisplay = [
{param: 'userName', displayName: 'User Name'},
{param: 'userEmail', displayName: 'User Email'}
];
$scope.resetSearchField = function(param){
$scope.searchParams[param.param] = "";
};
$scope.showParam = function(param){ ... }

