AngularJS自定义指令之复制指令实现方法
本文实例讲述了AngularJS自定义指令之复制指令实现方法。分享给大家供大家参考,具体如下:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" > <script src="jquery.min.js"></script> <script src="angular.js"></script> <script src="bootstrap.min.js"></script> <script type="text/javascript"> var app = angular.module('myapp', []); app.directive('duplicate', function($rootScope){ return { transclude: 'element', replace: true, priority: 2, link: function(scope, elem, attrs, ctrl, transclude) { var times = parseInt(attrs.duplicate); var previous = elem; var childScope; for(var i = 0; i < times; i++) { childScope = scope.$new();//新建scope的子scope childScope.number = i; transclude(childScope, function(clone){//transclude将每个childScope与clone关联, 一一对应(一对对) // console.log(childScope.$id); // console.log(clone.scope().$id) previous.after(clone);//在previous与clone拼接再赋值给clone previous = clone; }); } console.log(scope.childHead.number);//可以看到5个全是scope的子scopeconsole.log(scope.childTail.number); console.log(scope == $rootScope)//可以看到此时指令scope就是$rootScope 因为scope:false }, } }); </script> </head> <body ng-app="myapp"> <input type='text' ng-model='number' duplicate="5" /> <br/> <!--与上面绑定 --> <input type="text" ng-model="childHead.number"/><br/><inputtype="text"ng−model="childTail.number" /> </body> </html>
更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》
希望本文所述对大家AngularJS程序设计有所帮助。
相关推荐
youyouzouzou 2014-05-30
zbwroom 2014-05-30
lizean 2016-12-19
princejingyun 2016-12-14
youyouzouzou 2017-02-24
lizean 2017-09-06
拓宇 2017-09-06
问题描述在编写导入指令的时候,需要将函数绑定到指令中,并传入一个参数。<button ng-hide="importing" class="btn btn-warning btn-sm" type="
kkkder 2019-06-29
youyouzouzou 2017-09-09
Oranq 2017-09-06
lizean 2017-09-06
CrazyDogWang 2017-02-24
zhenghao 2016-12-19
CrazyDogWang 2016-12-14
Oranq 2016-11-14
CrazyDogWang 2016-07-22
Oranq 2016-06-15
Frankywls 2019-10-19
SakuraLu 2017-05-11