angularjs传递参数通过指令和NG重复 [英] angularjs passing parameters through directive and ng-repeat
问题描述
让我们再试一次:
这里的工作plunker:
http://plnkr.co/edit/RTrdsLY8ONoeDLPYSFJi?p=$p$pview一>
here's a working plunker: http://plnkr.co/edit/RTrdsLY8ONoeDLPYSFJi?p=preview
但是字段彼此连接。
当你看着DOM:
<input class="form-control ng-pristine ng-valid-maxlength ng-valid-minlength ng-valid ng-valid-required" type="text" name="fieldName" ng-model="fieldName" ng-minlength="3" ng-maxlength="20" required="">
^^^^^^^^^^
当然!所有有NAME =字段名!
of course! All have name="fieldName"!
但是,为什么!?它应该是名字,姓氏和年龄!
But why!? it should be first_name, last_name and age!
是的,有在formField.html模板错误:
Yes, there's a mistake in formField.html template:
<input class="form-control" type="{{fieldType}}" name="fieldName" ng-model="fieldName" ng-minlength=3 ng-maxlength=20 required/>
^^^^^^^^^^^
因此,让我们将它变成{{字段名}}。因此form。字段出现的所有......成表格。{{字段名}}
So let's change it into {{fieldName}}. And consequently all occurences of form.fieldName... into form.{{fieldName}}.
嘛,哪里走了红框?
让我们来看看DOM -
Let's look at DOM -
<input class="form-control ng-pristine ng-valid-maxlength ng-valid-minlength ng-valid ng-valid-required" type="text" name="first_name" ng-model="fieldName" ng-minlength="3" ng-maxlength="20" required="">
名称是好的,但现在有错不工作...
name is ok, but now has-error doesn't work...
推荐答案
更新::您可以使用这样的功能,从模型获取您的PARAMS。 (accroding你的数据结构)
Update: You can use a function like this to fetch your params from model. (accroding to your data structure)
另一个分叉plunker: http://plnkr.co/edit/mdKBoEffoUo2ilH7Jd3L?p = preVIEW
Another forked plunker: http://plnkr.co/edit/mdKBoEffoUo2ilH7Jd3L?p=preview
$scope.getModels = function(items) {
var params = {};
if (angular.isArray(items)) {
for (var i = 0; i < items.length; i++) {
var item = items[i];
if(angular.isArray(item.fields)) {
for (var j = 0; j < item.fields.length; j++) {
var field = item.fields[j];
params[field.id] = field.model;
}
}
}
}
alert(JSON.stringify(params, '', 4));
};
由于动态输入名称尚未这里尚未实施( Relatived PR ),我插入一个嵌套的 NG-形式
进行验证。而且因为如此,我认为你必须要管理形式使用submittion自己 ngModel
。
在这里检查分叉〔实施例: http://plnkr.co/edit/8cP5YMKUGu6LfBCsRxAZ p = preVIEW
Check the forked exmaple here: http://plnkr.co/edit/8cP5YMKUGu6LfBCsRxAZ?p=preview
模板:
<div class="form-group" ng-form="form" ng-class="{true: 'has-error', false: 'has-success'}[form.fieldName.$invalid]">
<label class="control-label col-sm-2" for="fieldName">{{fieldLabel}}</label>
<div class="col-sm-6">
<input class="form-control" type="{{fieldType}}" placeholder="enter valid name" name="fieldName" ng-model="fieldModel" ng-minlength=3 ng-maxlength=20 required/>
<span class="help-block">
<span ng-show="form.fieldName.$error.required">Required field</span>
<span ng-show="form.fieldName.$error.minlength">Too few chars - min is (6)</span>
<span ng-show="form.fieldName.$error.maxlength">Too much chars - max is (20)</span>
</span>
</div>
</div>
HTML
<form-field ng-repeat="field in tabItem.fields" ng-model='field.model'
field-type="field.type" field-name='field.id' field-label='field.label'>
</form-field>
JS:
KPNDirectives.directive("formField", function () {
return {
restrict: 'E',
scope: {
fieldModel: '=ngModel',
fieldType: '=',
fieldLabel: '=',
fieldName: '='
},
templateUrl: 'formField.html'
};
});
这篇关于angularjs传递参数通过指令和NG重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!