Angular implements automatic form layout and angularform Layout

Source: Internet
Author: User

Angular implements automatic form layout and angularform Layout

This article provides an example of how Angular implements automatic form layout and shares it with you for your reference. The specific content is as follows:

:

Code:

1. formlayoutCtrl. js

'Use strict '; sxlcApp. controller ('formlayoutctrl ', [' $ scope ',' $ filter', '$ http', function ($ scope, $ filter, $ http) {$ scope. title = 'form layout '; $ scope. dataParamsUrl = '. /php/formlayout. json '; $ scope. resetForm = function (num) {console. log (num)}]);

2. formlayout.html

<Div class = "container" ng-controller = "formlayoutCtrl"> <span >{{ title }}</span> <form class = "form-horizontal" style = "width: 400px; "name =" formlayout "w5c-form-validate =" validateOptions "novalidate> <form-layout url =" dataParamsUrl "> </form-layout> <button type =" submit "class =" btn btn-sm btn-info "> submit </button> <button type =" reset "class =" btn-sm btn-danger "ng-click =" resetForm () "> Reset </button> </form> </div>

3. formlayout.html

<Div class = "container" ng-controller = "formlayoutCtrl"> <span >{{ title }}</span> <form class = "form-horizontal" style = "width: 400px; "name =" formlayout "w5c-form-validate =" validateOptions "novalidate> <form-layout url =" dataParamsUrl "> </form-layout> <button type =" submit "class =" btn btn-sm btn-info "> submit </button> <button type =" reset "class =" btn-sm btn-danger "ng-click =" resetForm () "> Reset </button> </form> </div>

4. formlayoutdirective. js

'Use strict '; angular. module ('form. layout ', [])/*** defines the layout Service * @ method * @ param {Object}) this. defaultTemplate [layout TEMPLATE] * @ return {[type]} [description] */. provider ('formlayout ', function () {// defines the layout constructor function FormLayoutFn () {/*** defines the template of the form Element * @ type {text: string, radio: string, checkbox: string, remember: string, range: string, time: string, date: string, datetime: string, search: string, select: String} */this. elementTemplate = {text: '\ <div class = "form-group"> \ <label> </label> \ <input type = "text" class = "form-control"/>\</ div> ', password: '<div class = "form-group"> \ <label> </label> \ <input type = "password" class = "form-control"/>\</div> ', radio: '\ <div class = "form-group"> \ <label> </label> \ <div id = "radiolist"> </div> \ </div> ', email: '\ <div class = "form-group" >\< label> </label >\< Input type = "email" class = "form-control"/>\</div> ', tel: '\ <div class = "form-group"> \ <label> </label> \ <input type = "tel" class = "form-control"/>\</ div> ', url: '\ <div class = "form-group"> \ <label> </label> \ <input type = "url" class = "form-control"/>\</ div> ', number: '\ <div class = "form-group"> \ <label> </label> \ <input type = "number" class = "form-control"/>\</ div> ', checkbox: '<div class = "form-gro Up ">\< label> </label> \ <div id =" checkboxlist "> </div> \ </div> ', range: '<div class = "form-group"> \ <label> </label> \ <input type = "range"/>\</div>', time: '<div class = "form-group"> \ <label> </label> \ <input type = "time" class = "form-control"/>\</div> ', date: '<div class = "form-group"> \ <label> </label> \ <input type = "date" class = "form-control"/>\</div> ', datetime: '<div class = "form-group" >\< label> </ Label >\< input type = "datetime" class = "form-control"/>\</div> ', search: '<div class = "form-group"> \ <label> </label> \ <input type = "search" class = "form-control"/>\</div> ', select: '<div class = "form-group"> \ <label> </label> \ <select class = "form-control"> </select> \ </div>', textarea: '<div class = "form-group"> \ <label> </label> \ <textarea class = "form-control"> </textarea> \ <div> ', }; // default template. You can use the following Use the default template this. defaultTemplate = '<input>'; this. radioTmpl = '<label class = "radio-inline"> <input type = "radio"> radiotitle </label>'; this. checkboxTmpl = '<label class = "checkbox-inline"> <input type = "checkbox"> checkboxtitle </label>';} FormLayoutFn. prototype = {/*** get template * @ method getElementTemplate * @ return {[type]} [description] */getElementTemplate: function () {return this. elementTemplate ;}, /*** Configure the layout element template * @ param configTemplate */setElementTemplate: function (configTemplate) {this. elementTemplate = angular. extend (this. elementTemplate, configTemplate );}, /***** implement layout function * @ method layout * @ param {[type]} template object in the eleObj command * @ param {[type]} elementObj form layout element object * @ return {[type]} [description] */layout: function (eleObj, elementObj) {var thiz = this; var elementTemplate = this. elementT Emplate; var defaultTemplate = this. defaultTemplate; var radioTmpl = this. radioTmpl; var checkboxTmpl = this. checkboxTmpl; if (angular. isObject (eleObj) & angular. isObject (elementObj) {angular. forEach (elementObj, function (elementObjIterm, elementObjKey) {// console. log (elementObjIterm. attr. type); var type = $. trim (elementObjIterm. attr. type); var templateObj = angular. element (elementTemplate [type] ); // Console. log (templateObj. find ('input'); switch (type) {case 'textarea ': var fileld = templateObj. find ('textarea '); // var templateObj = angular. element (elementTemplate. textarea); break; case 'select': var fileld = templateObj. find ('select'); // var templateObj = angular. element (elementTemplate. select); break; case 'button ': var fileld = templateObj. find ('button '); // var templateObj = angu Lar. element (elementTemplate. button); break; case 'dateicker': var fileld = templateObj. find ('dateicker'); // var templateObj = angular. element (elementTemplate. datepicker); break; case 'Radio ': var fileld = templateObj. find ('# radiolist'); // var templateObj = angular. element (elementTemplate. datepicker); break; case 'checkbox': var fileld = templateObj. find ('# checkboxlist'); // var templateObj = Angular. element (elementTemplate. datepicker); break; default: var fileld = templateObj. find ('input'); break;} // set the label name templateobj.find('label'demo.html (elementObjIterm. labeltext); if ('select' = type) {var options = elementObjIterm. attr. option; angular. forEach (options, function (content, val) {var option = angular. element ('<option value = "' + val + '">' + content + '</option>'); fileld. append (o Ption) ;}) ;}else if ('Radio '= type) {var options = elementObjIterm. attr. option; angular. forEach (options, function (content, val) {var tmpl = radioTmpl. replace ('radiotitle', content); var tmplObj = angular. element (tmpl); tmplObj. find ('input '). attr ('value', val); fileld. append (tmplObj) ;}); // console. log (templateObj. find ('label');} else if ('checkbox' = type) {var options = elementObjIterm. attr. o Ption; angular. forEach (options, function (content, val) {var tmpl = checkboxTmpl. replace ('checkboxtitle', content); var tmplObj = angular. element (tmpl); tmplObj. find ('input '). attr ('value', val); fileld. append (tmplObj) ;});} else {angular. forEach (elementObjIterm. attr, function (val, attrname) {fileld. attr (attrname, val) ;}} eleObj. append (templateObj. append (fileld);}); return eleObj;} else {throw 'The input parameter is not an object' ;}}; // The instance layout construction class var formLayout = new FormLayoutFn (); this. $ get = function () {return formLayout;}; // configure the layout element template this. setElementTemplate = function (configTemplate) {if (! ConfigTemplate) return; formLayout. setElementTemplate (configTemplate) ;}});/*** command implementation * @ method * @ return {[type]} [description] */angular. module ('form. layout '). directive ('formlayout ', [' $ http', '$ filter', 'formlayout', function ($ http, $ filter, formLayout) {return {restrict: 'AE ', scope: {url: "=", // fields :{}}, replace: true, // templateUrl :'. /tmpl/formlayout.html ', transclude: true, // Require :'? ^ FormLayout ', link: function (scope, elem, attrs) {if (! Scope. url) {throw' enter the url value of the obtained data in the url of the instruction parameter ';} $ http. get (scope. url ). success (function (successData, status, headers, config) {if (! SuccessData. code) {scope. fields = successData. data; processFormFilds (successData. data);} else {throw' failed to get form data ';}) function processFormFilds (data) {formLayout. layout (elem, data); // console. log (data) ;}}}]);

The above is the detailed code for Angular to implement automatic form layout. I hope it will be helpful for your learning.

Articles you may be interested in:
  • AngularJS form submission (form)
  • Form-data for uploading Angular Js files
  • AngularJS uses angular-formly for Form Verification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.