zoukankan      html  css  js  c++  java
  • AngularJS clone directive 指令复制

    需求背景:

            directive模块化某表单信息,但表单信息可加入多条。此时就涉及到clone directive.

    解决方式:

            能够通过使用angularjs中$compile来进行clone directive。clone direcitve中常涉及到数据的绑定。

    详细方法:

        tw.factory('DirectiveUtil', [function() {
          var DirectiveUtil = {};
          
          DirectiveUtil.DirectiveBuilder = function(directiveName) {
          
            directive = directiveName;
            directiveBuffer = '<' + directiveName + ' ';
    
            this.setDirectiveName = function(directiveName) {
              directive = directiveName;
              directiveBuffer = '<' + directiveName + ' ';
            }
    
            this.getDirectiveName = function() {
              return directive;
            }
            // name: directive中scope的name, value: clone directive操作时,数据绑定的名称
            this.appendAttr = function(name, value) {
              directiveBuffer += name + '='' + value + '' ';
              return this;
            }
    
            this.build = function(compile, scope) {
              return compile(directiveBuffer + ' />')(scope);
            }
          };
          
          return DirectiveUtil;
        }]);

    使用方式:

      var dirctBuilder = new DirectiveUtil.DirectiveBuilder('tw-contact-form');
      dirctBuilder.appendAttr('is-new', 'isNew')     // $scope.isNew, $scope.showAddBtn, $scope.initData
          .appendAttr('show-add-btn', 'showAddBtn')
          .appendAttr('init-data', 'initData');
      
      var li = $('<li></li>').attr('id', 'ContactList' + len);
      li.append(dirctBuilder.build($compile, $scope));

    注意:引入DirectiveUtil,调用build时传入$compile和$scope。

  • 相关阅读:
    js看懂funid与funname各自的作用以及回显示是怎么找到对应位置的
    居然脑海中还记得03年的瑞星升级ID号
    [恢]hdu 2186
    [恢]hdu 2109
    [恢]hdu 2140
    [恢]hdu 2160
    [恢]hdu 2101
    [恢]hdu 2162
    [恢]hdu 2201
    [恢]hdu 2163
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5273145.html
Copyright © 2011-2022 走看看