需求背景:

        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。

AngularJS clone directive 指令复制的更多相关文章

  1. AngularJS中Directive指令系列 - scope属性的使用

    文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...

  2. AngularJS中Directive指令系列 - 基本用法

    参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...

  3. 关于Angularjs写directive指令传递参数

    包子又来啦.... 在Angularjs当中,我们可能会经常要写directive指令.但是指令如果要共用的话,肯定是有细微的差别的,所以这些差别可能需要一个参数来决定 所以如何在指令中传递参数呢.. ...

  4. angularjs中directive指令与component组件有什么区别?

     壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...

  5. Angularjs之directive指令学习笔记(二)

    1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...

  6. 【angularJS】Directive指令

    AngularJS 通过被称为 指令 的新属性来扩展 HTML.指令是扩展的 HTML 属性,带有前缀 ng-. 内置指令 1.ng-app 指令初始化一个 AngularJS 应用程序. 定义了 A ...

  7. angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)

    angular js 中模板的使用.事件绑定以及指令与指令之间的交互 相应教学视频地址(需FQ):v=aG8VD0KvUw4">angularjs教学视频 <!doctype h ...

  8. AngularJS中Directive指令系列 - bindToController属性的使用

    默认false.这个属性用来绑定scope的属性直接赋给controller.可以为true或者和scope相同格式的对象. 此外使用此属性,要设置controller的别名,通常通过"co ...

  9. AngularJS中Directive指令系列

    近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用.  &, <, =, @ 符 ...

随机推荐

  1. UIImageView添加边框和阴影

    - (void)viewDidLoad { [super viewDidLoad]; //添加显示 UIImage *image = [UIImage imageNamed:@"0_wang ...

  2. python下 help()使用方法

    查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules: help("modules ...

  3. Linux下python3与python3的多版本共存

    python3已经出来有些许时候了,python3相比python2进行了大量的改进,包括语法,新的功能,还有优化.虽然很多库已经同时支持 python2和python3了,但是有些库仍然没有很好的支 ...

  4. springmvc数组参数传递

    在开发中遇到将form中的name值一样的多个input元素传递到后台,我用的是springmvc. 刚开始的时候老是报400的请求错误.后来查了下资料,其实解决方案挺简单的. 我的后台control ...

  5. SOLID (面向对象设计) 基本原则

      SOLID (面向对象设计) 基本原则    在 程序设计领域, SOLID (单一功能.开闭原则.里氏替换.接口隔离以及依赖反转)是由罗伯特•C•马丁在21世纪早期[1] 引入的记忆术首字母缩略 ...

  6. linux 命令——PS命令

    首先要明白什么是进程进程是一个正在运行的程序.也就是说在Linux系统上运行的所有东西都是进程.和进程相比较,作业是一系列按一定顺序执行的命令.一条简单的命令可能会涉及多个进程,尤其是当使用管道和重定 ...

  7. xcode5 自定义模板

    经过一番周折,终于在xcode5上实现了一个简单的自定义模板,在项目中集成NSLogger库(增强NSLog的功能,https://github.com/fpillet/NSLogger)——新建项目 ...

  8. Xcode 快捷键-b

    0.跳到指定行command + L; 1.配置高亮方案:Fonts & Colors 2.键盘快捷键:Key Bindings 3.代码缩进:Re-indent selection.(快捷键 ...

  9. IDE 常用快捷键记录

    一:Eclipse (1)删除当前行:Ctrl+D (2)最大化当前编辑窗口:Ctrl+m (3)关闭当前编辑器窗口:Ctrl+F4/Ctrl+w (4)导入依赖包:Ctrl+Shift+o 二:Ne ...

  10. 利用Asio搭建日志系统

    Asio(http://think-async.com)官方示例中给出了一个比较初步的日志服务,主要代码在basic_logger.hpp.logger_service.hpp.logger_serv ...