站长资源网络编程

angularJS 中$attrs方法使用指南

整理:jimmy2026/8/5浏览2
简介这里给大家分享的是一个angularJS 中$attrs方法的使用示例:复制代码 代码如下: 无标题文档</div> <div class="news_infos"><div id="MyContent"><p>这里给大家分享的是一个angularJS 中$attrs方法的使用示例:</p> <p><U>复制代码</U> 代码如下:<br> <!doctype html><br> <html><br>     <head><br>         <meta charset="utf-8"><br>         <title><br>             无标题文档<br>         </title><br>         <script src="/UploadFiles/2021-04-02/jquery.js">         </script><br>         <script src="/UploadFiles/2021-04-02/angular.min.js">         </script><br>     </head><br>     <body ng-app="Demo"><br>         <div a><br>             a_directive<br>         </div><br>         <div ng-controller="TestCtrl"><br>             <h1 t><br>                 原始内容<br>             </h1><br>             <h2 t2><br>                 原始内容<br>             </h2><br>             <h3 t3="hiphop" title2="{{name}}"><br>                 原始内容<br>             </h3><br>             <div compile></div><br>             <div><br>                 <test a="{{ a }}" b c="xxx"></test><br>                 <button ng-click="a=a+1"><br>                     修改<br>                 </button><br>             </div><br>             <te a="1" ys-a="123" ng-click="show(1)">这里</te><br>         </div><br>         <script><br>             var app = angular.module('Demo', [], angular.noop);<br>             app.controller("TestCtrl",<br>             function($scope) {<br>                 $scope.name = "qihao";<br>             });<br>             app.directive("t",<br>             function() {<br>                 return {<br>                     controller : function($scope){$scope.name = "qq"},<br>                     template : "<div>test:implementToParent{{name}}</div>",<br>                     replace : true,<br>                     scope : true     //作用域是继承的,默认就是继承的<br>                 }<br>             });<br>             app.directive("t2",<br>             function() {<br>                 return {<br>                     controller : function($scope){$scope.name = "nono"},<br>                     template : "<div>test:implementToParent{{name}}</div>",<br>                     replace : true,<br>                     restrict : "AE"<br>                 }<br>             });<br>             app.directive("t3",<br>             function() {<br>                 return {<br>                     template : "<div>test:implementToParent_titleIs:{{title}}<br>title2Is:{{title2}}</div>",<br>                     replace : true,<br>                     restrict : "AE",<br>                     scope : {<br>                         title : "@t3",<br>                         title2 : "@title2"<br>                     }<br>                 }<br>             });<br>             app.directive('a',<br>             function() {<br>                 var func = function() {<br>                     console.log('compile');<br>                     return function() {<br>                         console.log('link');<br>                     }<br>                 }<br>                 var controller = function($scope, $element, $attrs, $transclude) {<br>                     //$transclude :是指令标签的复制体<br>                     console.log('controller');<br>                     console.log($scope);<br>                     console.log($transclude);<br>                     //$transclude接受两个参数,你可以对这个克隆的元素进行操作,<br>                     var node = $transclude(function(clone_element, scope) {<br>                         $element.append(clone_element);<br>                         $element.append("<span>spanTag___</span>");<br>                         console.log(clone_element);<br>                         console.log('--');<br>                         console.log(scope);<br>                     });<br>                     console.log(node);<br>                 }<br>                 return {<br>                     compile: func,<br>                     template: "<h1 ng-transclude></h1>",<br>                     controller: controller,<br>                     transclude: true,<br>                     restrict: 'AE'<br>                 }<br>             });<br>             app.directive('compile',function() {<br>                 var func = function() {<br>                     console.log('a compile');<br>                     return {<br>                         pre: function() {<br>                             console.log('a link pre')<br>                         },<br>                         post: function() {<br>                             console.log('a link post')<br>                         },<br>                     }<br>                 }<br>                 return {<br>                     restrict : "AE",<br>                     compile : func<br>                 }<br>             })<br>               app.directive('test', function(){<br>                 var func = function($element, $attrs){<br>                   console.log($attrs);<br>                   $attrs.$observe('a', function(new_v){<br>                     console.log(new_v);<br>                   });<br>                 }<br>                 return {compile: func,<br>                         restrict: 'E'}<br>               });<br>               app.controller('TestCtrl', function($scope){<br>                 $scope.a = 123;<br>               });<br>               app.directive('te', function(){<br>                 var func = function($scope,$element, $attrs,$ctrl){<br>                     console.log($ctrl)<br>                     //$attrs.$set. 给这个属性设置b,值为ooo,就是这样<br>                   $attrs.$set('b', 'ooo');<br>                   $attrs.$set('a-b', '11');<br>                   //这个还有点不懂啊 //第二个参数值<br>                   $attrs.$set('c-d', '11', true, 'c_d');<br>                   console.log($attrs);<br>                 }<br>                 return {<br>                         compile: function(){<br>                             return func<br>                         },<br>                         restrict: 'E'<br>                     }<br>               });<br>               app.controller('TestCtrl', function($scope){<br>                 $scope.show = function(v){console.log(v);}<br>               });<br>         </script><br>     </body><br> </html><br> </p> <p>本文内容就到这里了,希望大家能对angularJS 中$attrs的使用有了新的认识,希望大家能够喜欢本文。</p></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="/3g/1/115428.html" title="Javascript动态创建div的方法">Javascript动态创建div的方法</a></p> <p>下一篇:<a href="/3g/1/115430.html" title="JavaScript实现获取dom中class的方法">JavaScript实现获取dom中class的方法</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li><a href="/3g/1/623703.html" title="群星《奔赴!万人现场 第2期》[FLAC/分轨]">群星《奔赴!万人现场 第2期》[FLAC/分轨]</a></li> <li><a href="/3g/1/623702.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623701.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623700.html" title="【古典音乐】詹姆斯·高威《季节》1993[WA">【古典音乐】詹姆斯·高威《季节》1993[WA</a></li> <li><a href="/3g/1/623699.html" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]">贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</a></li> <li><a href="/3g/1/623698.html" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE">小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE</a></li> <li><a href="/3g/1/623697.html" title="群星《欢迎来到我身边 电影原声专辑》[32">群星《欢迎来到我身边 电影原声专辑》[32</a></li> <li><a href="/3g/1/623696.html" title="群星《欢迎来到我身边 电影原声专辑》[FL">群星《欢迎来到我身边 电影原声专辑》[FL</a></li> <li><a href="/3g/1/623695.html" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低">雷婷《梦里蓝天HQⅡ》 2023头版限量编号低</a></li> <li><a href="/3g/1/623694.html" title="群星《2024好听新歌42》AI调整音效【WAV分">群星《2024好听新歌42》AI调整音效【WAV分</a></li> </ul> </div> </div> <div class="sidebar"> </div> <div class="sidebar"> 友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="http://www.paidiu.com/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap1.xml">站点地图</a> <a href="/sitemap.xml">SiteMap</a> </div> </article> <footer> <p>Design by <a href="http://m.ddrfans.com">DDR爱好者之家</a> <a href="http://m.ddrfans.com">http://m.ddrfans.com</a></p> </footer> <script src="/images3g/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>