最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular實(shí)現(xiàn)form自動布局

 更新時(shí)間:2016年01月28日 16:43:14   作者:anziguoer  
這篇文章主要介紹了Angular實(shí)現(xiàn)form自動布局的相關(guān)資料,以代碼片段的形式分析了Angular實(shí)現(xiàn)form自動布局的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下

本文實(shí)例講解了Angular實(shí)現(xiàn)form自動布局的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下

效果圖:

具體代碼:

1.formlayoutCtrl.js

'use strict';
sxlcApp.controller('formlayoutCtrl', ['$scope', '$filter', '$http', function($scope, $filter, $http){
 $scope.title = '表單布局中';
 $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">
   提交
  </button>
  <button type="reset" class="btn btn-sm btn-danger" ng-click="resetForm()">
   重置
  </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">
   提交
  </button>
  <button type="reset" class="btn btn-sm btn-danger" ng-click="resetForm()">
   重置
  </button>
 </form>
</div>

4. formlayoutdirective.js 

'use strict';
angular.module('form.layout', [])
/**
 * 定義布局的服務(wù)
 * @method
 * @param {Object} ) this.defaultTemplate [布局的模板]
 * @return {[type]}  [description]
 */
.provider('formLayout', function(){
 // 定義布局構(gòu)造函數(shù) 
 function FormLayoutFn(){
  /**
   * 定義表單元素的模板
   * @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-group">\
        <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>',
   };
  //默認(rèn)的模板, 可以使用如下的方式使用默認(rèn)的模板
  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 = {
  /**
   * 獲取模板
   * @method getElementTemplate
   * @return {[type]}   [description]
   */
  getElementTemplate : function () {
   return this.elementTemplate;
  },
 
  /**
   * 配置布局元素的模板
   * @param configTemplate
   */
  setElementTemplate : function(configTemplate){
   this.elementTemplate = angular.extend(this.elementTemplate, configTemplate);
  },
 
  /**
   * 實(shí)現(xiàn)布局函數(shù)
   * @method layout
   * @param {[type]} eleObj  指令中的模板對象
   * @param {[type]} elementObj 表單布局元素對象
   * @return {[type]}   [description]
   */
  layout : function(eleObj, elementObj){
   var thiz = this;
   var elementTemplate = this.elementTemplate;
   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 = angular.element(elementTemplate.button);
       break;
      case 'datepicker' :
       var fileld = templateObj.find('datepicker');
       // 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;
     }
     // 設(shè)置 label 的標(biāo)簽名字
     templateObj.find('label').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(option);
      });
     }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.option;
      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 '傳入的參數(shù)不是對象';
   }
    
  }
 
 };
 // 實(shí)例布局化構(gòu)造類
 var formLayout = new FormLayoutFn();
 
 this.$get = function () {
  return formLayout;
 };
 //配置布局元素的模板
 this.setElementTemplate = function (configTemplate) {
  if (!configTemplate) return ;
  formLayout.setElementTemplate(configTemplate);
 }
});
/**
 * 指令的實(shí)現(xiàn)
 * @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 '請?jiān)谥噶顓?shù)url中傳入獲取數(shù)據(jù)的 url 的值';
    }
    $http.get(scope.url).success(function(successData, status, headers, config){
     if (!successData.code) {
      scope.fields = successData.data;
      processFormFilds(successData.data);
     }else{
      throw '獲取表單數(shù)據(jù)失敗';
     }
    })
 
    function processFormFilds(data){
     formLayout.layout(elem, data);
     // console.log(data);
    }
   }
  }
 }]);

以上就是Angular實(shí)現(xiàn)form自動布局的詳細(xì)代碼,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • 詳解angularjs的數(shù)組傳參方式的簡單實(shí)現(xiàn)

    詳解angularjs的數(shù)組傳參方式的簡單實(shí)現(xiàn)

    本篇文章主要介紹了angularjs的數(shù)組傳參方式的簡單實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • AngularJS入門教程之?dāng)?shù)據(jù)綁定用法示例

    AngularJS入門教程之?dāng)?shù)據(jù)綁定用法示例

    這篇文章主要介紹了AngularJS之?dāng)?shù)據(jù)綁定用法,結(jié)合實(shí)例形式分析了AngularJS基于內(nèi)置指令ng-model實(shí)現(xiàn)數(shù)據(jù)綁定的操作技巧,需要的朋友可以參考下
    2016-11-11
  • Angular實(shí)現(xiàn)響應(yīng)式表單

    Angular實(shí)現(xiàn)響應(yīng)式表單

    本篇文章主要介紹了Angular實(shí)現(xiàn)響應(yīng)式表單,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • AngularJS 使用ng-repeat報(bào)錯(cuò) [ngRepeat:dupes]

    AngularJS 使用ng-repeat報(bào)錯(cuò) [ngRepeat:dupes]

    這篇文章主要介紹了AngularJS 使用ng-repeat報(bào)錯(cuò) [ngRepeat:dupes] 的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • AngulerJS學(xué)習(xí)之按需動態(tài)加載文件

    AngulerJS學(xué)習(xí)之按需動態(tài)加載文件

    本篇文章主要介紹了AngulerJS學(xué)習(xí)之按需動態(tài)加載文件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • 最新評論

    抚顺市| 徐州市| 滨州市| 西丰县| 务川| 高台县| 辽阳县| 寿宁县| 大埔县| 灵寿县| 七台河市| 平远县| 芷江| 太康县| 镇平县| 东港市| 青阳县| 内乡县| 山阴县| 永川市| 大渡口区| 吉林市| 隆回县| 河西区| 会同县| 麦盖提县| 潼关县| 绩溪县| 玛多县| 溧水县| 四子王旗| 新郑市| 得荣县| 高淳县| 新干县| 泰兴市| 鹤山市| 陇西县| 台中县| 瑞安市| 蒙城县|