我尝试使用JSON中的数据在AngularJS中创建动态表单。我有这个工作:
HTML
<p ng-repeat="field in formFields">
<input
dynamic-name="field.name"
type="{{ field.type }}"
placeholder="{{ field.name }}"
ng-model="field.value"
required
>
<span ng-show="myForm.{{field.name}}.$dirty && myForm.{{field.name}}.$error.required">Required!</span>
<span ng-show="myForm.{{field.name}}.$dirty && myForm.{{field.name}}.$error.email">Not email!</span>
</p>
<button ng-disabled="myForm.$invalid">Submit</button>
</form>
JS
angular.module('angularTestingApp').controller('DynamicFormCtrl', function ($scope) {
$scope.formFields = [
{
name: 'firstName',
type: 'text'
},
{
name: 'email',
type: 'email'
},
{
name: 'password',
type: 'password'
},
{
name: 'secondName',
type: 'text'
}
];}).directive("dynamicName",function($compile){
return {
restrict:"A",
terminal:true,
priority:1000,
link:function(scope,element,attrs){
element.attr('name', scope.$eval(attrs.dynamicName));
element.removeAttr("dynamic-name");
$compile(element)(scope);
}
}});
这段代码有效,但是问题是 我不知道如何添加动态复选框或清单 以及如何在表单内部进行 验证 ,如下所示:
angular.module('angularTestingApp')
.controller(’DynamicFormCtrl’,function($ scope){
$scope.formFields = [
{
name: 'firstName',
type: 'text'
},
{
name: 'email',
type: 'email'
},
{
name: 'password',
type: 'password'
},
{
name: 'city',
type: 'checkbox',
choices: [
{ name: "red" },
{ name: "blue" },
{ name: "green" },
]
}
];})
预先感谢您的关注。最好的祝福!
我已经解决了我的问题。
这是一个带有AngularJS中带有验证的动态表单示例的链接
http://plnkr.co/edit/kqiheTEoGDQxAoQV3wxu?p=preview
.html
<form name="myForm" class="form-horizontal" role="form" ng-submit="submitForm()">
<div ng-repeat="field in entity.fields">
<ng-form name="form">
<!-- TEXT FIELDS -->
<div ng-if="field.type=='text'" class="form-group">
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<input type="{{ field.type }}" dynamic-name="field.name" id="{{field.name}}" data-ng-model="field.data" class="form-control" required/>
<!--<span ng-show="myForm.{{field.name}}.$dirty && myForm.{{field.name}}.$error.required">Required!</span>.-->
<span data-ng-show=" {{'form.'+field.name+'.$dirty && form.'+field.name+'.$error.required'}}">Required!</span>
</div>
</div>
<!-- EMAIL FIELDS -->
<div ng-if="field.type=='email'" class="form-group">
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<input type="{{ field.type }}" dynamic-name="field.name" data-ng-model="field.data" class="form-control" required/>
<span data-ng-show=" {{'form.'+field.name+'.$dirty && form.'+field.name+'.$error.required'}}">Required!</span>
<span data-ng-show=" {{'form.'+field.name+'.$dirty && form.'+field.name+'.$error.email'}}">Not email!</span>
</div>
</div>
<!-- PASSWORD FIELDS -->
<div ng-if="field.type=='password'" class="form-group" >
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<input type="{{ field.type }}" dynamic-name="field.name" data-ng-model="field.data" ng-minlength={{field.min}} ng-maxlength={{field.max}} class="form-control" required/>
<span data-ng-show=" {{'form.'+field.name+'.$dirty && form.'+field.name+'.$error.required'}}">Required!</span>
<span data-ng-show=" {{'!form.'+field.name+'.required && (form.'+field.name+'.$error.minlength || form.'+field.name+'.$error.maxlength)' }}">Passwords must be between 8 and 20 characters.</span>
</div>
</div>
<!-- SELECT FIELDS -->
<div ng-if="field.type=='select'" class="form-group" >
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<select data-ng-model="field.data" ng-options="option.name for option in field.options" class="form-control" required/>
</div>
</div>
<!-- RADIO FIELDS -->
<div ng-if="field.type=='radio'" class="form-group">
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<div class="checkbox" ng-repeat="option in field.options" >
<label>
<input type="radio" data-ng-model="field.data" name="taskGroup" id="{{option.name}}" value="{{option.id}}">{{option.name}}
</label>
</div>
</div>
</div>
<!-- CHECKBOX FIELDS -->
<div ng-if="field.type=='checkbox'" class="form-group" >
<label class="col-sm-2 control-label">{{field.label}}</label>
<div class="col-sm-6">
<div class="checkbox" ng-repeat="option in field.options" >
<label>
<input type="checkbox" data-ng-model="option.data" name="taskGroup" id="{{option.name}}" value="{{option.id}}" >{{option.name}}
</label>
</div>
</div>
</div>
</ng-form>
</div>
<br/>
<button ng-disabled="myForm.$invalid" type="submit" id="submit">Submit</button>
<br/>
<pre>{{entity|json}}</pre>
<br/>
</form>
.js
app.controller('DynamicFormController', function ($scope, $log) {
// we would get this from the api
$scope.entity = {
name : "Course",
fields :
[
{type: "text", name: "firstname", label: "Name" , required: true, data:""},
{type: "radio", name: "color_id", label: "Colors" , options:[{id: 1, name: "orange"},{id: 2, name: "pink"},{id: 3, name: "gray"},{id: 4, name: "cyan"}], required: true, data:""},
{type: "email", name: "emailUser", label: "Email" , required: true, data:""},
{type: "text", name: "city", label: "City" , required: true, data:""},
{type: "password", name: "pass", label: "Password" , min: 6, max:20, required: true, data:""},
{type: "select", name: "teacher_id", label: "Teacher" , options:[{name: "Mark"},{name: "Claire"},{name: "Daniel"},{name: "Gary"}], required: true, data:""},
{type: "checkbox", name: "car_id", label: "Cars" , options:[{id: 1, name: "bmw"},{id: 2, name: "audi"},{id: 3, name: "porche"},{id: 4, name: "jaguar"}], required: true, data:""}
]
};
$scope.submitForm = function(){
$log.debug($scope.entity);
}
})
.directive("dynamicName",function($compile){
return {
restrict:"A",
terminal:true,
priority:1000,
link:function(scope,element,attrs){
element.attr('name', scope.$eval(attrs.dynamicName));
element.removeAttr("dynamic-name");
$compile(element)(scope);
}
}
})
我想创建一个从Api读取数据的图表。要做到这一点,我有这把小提琴http://jsfiddle.net/68oe1oLf/69/ 注意:https://jsfiddle.net/68oe1oLf/69/将导致混合内容错误,并且无法从api加载数据 这是javascript代码 我从这个文档示例中借用了这个想法http://jsfiddle.net/gh/get/library/pure/highc
问题内容: 当要依赖的测试与具有此批注的测试属于同一类时,批注的属性可以正常工作。但是,如果要测试的方法和依赖的方法位于不同的类中,则该方法不起作用。示例如下: 有什么办法可以解决这个限制?一种简单的解决方法是在该调用中创建测试。但这将是过多的重复。 问题答案: 将方法放在中并使用。 建议验证*中的配置,并在该处出现问题时抛出错误,以使测试无法运行。这样,测试可以只关注测试。
本文向大家介绍AngularJS自动表单验证,包括了AngularJS自动表单验证的使用技巧和注意事项,需要的朋友参考一下 AngularJS的另外一种表单验证方式是自动验证,即通过directive来实现,除了AngularJS自带的directive,还需要用到angular-auto-validate这个第三方module。 有关angular-auto-validate: 安装:npm i
我有Java POJO课是这样的: 我有一个像这样的Kotlin数据课 如何向的任何变量提供,如Java变量中的注释?
我有一个用例,我必须合并来自2个表(位于不同的数据源中)的数据。考虑到每个表都有员工数据(名字、姓氏、phoneNumber)。对于每个员工,我必须合并来自两个表的phoneNumber数据,其他数据将与表1中的相同 如果表1和表2中都有员工数据,则电话号码数据将合并为逗号分隔的值,其他数据将来自表1 这些表每个表大约有40行缺少数据。接近5GB。在Java中实现这一点的最佳方法是什么?我担心的是
问题内容: 给定一个像这样的json: 和两个常规的html输入: 我需要建立一个包含所有可能变化形式的表,例如: 这意味着,如果用户继续通过输入添加值,则将出现建立新变体的新行,例如: 我还需要有可用的id来处理它,并且当我使用输入(例如“Peter”“Black”)添加新值时,我需要像自动递增那样动态地自动填充id(颜色id)mysql,结果如下: 那可能吗?我可以使用哪些选项进行角度操作?我