AngularJS clone directive 指令复制
需求背景:
解决方案:
具体方法:
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));
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。