AngularJS自定义指令之复制指令实现方法
程序员文章站
2022-06-25 11:00:34
本文实例讲述了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程序设计有所帮助。
推荐阅读