[Angular] ngx-formly (AKA angular-formly for Angular latest version)
In our dynamic forms lessons we obviously didn’t account for all the various edge cases you might come across. Therefore if you need a more complex setup you might want to take a look at ngx-formly. Formly has been a very popular library even in AngularJS 1.x for rendering dynamic forms.
Ngx-formly is the Angular (2+) counterpart for doing this job. In this lesson we’ll quickly give it a look at how to transform our custom dynamic form rendering to use ngx-formly. For more use cases definitely check out the official ngx-formly repository on GitHub.
Add module to the root:
import {NgModule} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {FormlyModule} from '@ngx-formly/core';
import {FormlyBootstrapModule} from '@ngx-formly/bootstrap';
// for material2 import `FormlyMaterialModule`:
// import {FormlyMaterialModule} from '@ngx-formly/material';
@NgModule({
imports: [
...,
ReactiveFormsModule,
FormlyModule.forRoot(),
FormlyBootstrapModule,
// for material2 use:
// FormlyMaterialModule
],
})
export class AppModule {}
One difference from angular-formly with ngx-formly is the validation:
import { Validators } from '@angular/forms';
import { Component } from '@angular/core';
import { FormlyFieldConfig } from '@ngx-formly/core';
@Component({
selector: 'app-root',
template: `
<h1>Dynamic reactive forms in Angular</h1>
<app-dynamic-form [data]="person" [formDataObj]="personFields"></app-dynamic-form>
`
})
export class AppComponent {
person = {
firstname: 'Juri',
age: 32,
gender: 'M'
};
personFields = <FormlyFieldConfig>[
{
key: 'firstname',
type: 'input',
templateOptions: {
label: 'Firstname'
},
validators: {
validation: Validators.required
},
validation: {
messages: {
required: 'You need to provide a value'
}
}
},
{
key: 'age',
type: 'input',
templateOptions: {
label: 'Age',
type: 'number'
},
validators: {
validation: Validators.min(18)
},
validation: {
messages: {
min: 'You need to specify a value greater or equal to 18'
}
}
},
{
key: 'gender',
type: 'radio',
templateOptions: {
label: 'Gender',
options: [{ value: 'Male', key: 'M' }, { value: 'Female', key: 'F' }]
}
}
];
}
More infromation to check out.
[Angular] ngx-formly (AKA angular-formly for Angular latest version)的更多相关文章
- 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...
- Angular 个人深究(一)【Angular中的Typescript 装饰器】
Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...
- (转载)从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文是入门篇.笔者认为亲自动手写代码做实验,是最有效最扎实的学习途径,而搭建开发环境是学习一门新技术最需要先学会的技能,是入门的前提. ...
- angular.module()创建、获取、注册angular中的模块
// 传递参数不止一个,代表新建模块;空数组代表该模块不依赖其他模块 var createModule = angular.module("myModule", []); // 只 ...
- Angular入门,开发环境搭建,使用Angular CLI创建你的第一个Angular项目
前言: 最近一直在使用阿里的NG-ZORRO(Angular组件库)开发公司后端的管理系统,写了一段时间的Angular以后发现对于我们.NET后端开发而言真是非常的友善.因此这篇文章主要是对这段时间 ...
- [Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer
By default, when you generate components, they will simply be added to the page in order, one after ...
- [Angular 2] Set Properties on Dynamically Created Angular 2 Components
When you generate Angular 2 components, you’re still able to access the component instance to set pr ...
- [转]angular官网 及 Ant Design of Angular
https://angular.io/cli https://www.angular.cn/guide/quickstart https://ng.ant.design/docs/introduce/ ...
- [AngularJS] Angular 1.3 $submitted for Form in Angular
AngularJS 1.3 add $submitted for form, so you can use $submitted to track whether the submit event ...
随机推荐
- Centos安装FastDFS+Nginx
一.安装环境: gcc:安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc: yum install gcc-c++ PCRE:PCRE(Perl C ...
- hdu 1002(大数)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Node.js:多进程
ylbtech-Node.js:多进程 1.返回顶部 1. Node.js 多进程 我们都知道 Node.js 是以单线程的模式运行的,但它使用的是事件驱动来处理并发,这样有助于我们在多核 cpu 的 ...
- 根据日期获取,x岁x月x天
c#: DateTime startDate = new DateTime(); DateTime endDate = new DateTime(); ; ; ; if (endDate.Month& ...
- 第6章 服务模式 在 .NET 中实现 Service Interface
上下文 您 的应用程序部署在 Microsoft Windows? 操作系统上.您决定将应用程序的某一块功能作为 ASP.NET Web Service 公开.互操作性是一个关键问题,因此您无法使用仅 ...
- 超级简单的利用javascript实现文件拖拽事件
1.效果图: 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %& ...
- Java程序连接各种数据库的driver和url形式
1.Oracle数据库 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url = & ...
- GoogleMap 获取自己的数字证书API key的步骤
http://dreamylights.blog.51cto.com/1163218/1360759 1. 进入到Google APIs Console页面 https://code.google.c ...
- Windows下PHP的redis扩展下载地址
Redis下载地址:http://windows.php.net/downloads/pecl/releases/redis/ igbinary下载地址:http://windows.php.net/ ...
- MIME类型记录
Content-Disposition: attachment; filename="filename.xls" 提供下载