国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Angular.js自定義指令學習筆記實例

2024-05-06 15:10:50
字體:
來源:轉載
供稿:網友

本文給大家分享angular.js學習筆記之自定義指令實例代碼講解,具體代碼如下所示:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>AngularDirective</title><script src="http://cdn.bootcss.com/angular.js/1.4.6/angular.js"></script></head><body ng-app="angularJS" ><!-- <div class="self-direct">{{title}}<input type="text" ng-model='title'></div> --><!-- <input type="text" ng-model="color"><self-direct color='{{color}}'></self-direct><self-direct m-color='{{color}}'></self-direct> --><!-- <input type="text" ng-model="color"><self-direct color='color'></self-direct>如果采用雙向綁定,指令中的屬性值默認是變量,所以不用添加{{}}<self-direct m-color='color'></self-direct> --><!-- <self-direct logo='logo()'></self-direct> --><!-- <self-direct ></self-direct> --><!-- <self-direct ></self-direct> --><self-direct title="JinDong" bgcolor="red" fontcolor="#fff"></self-direct><script type="text/javascript">/*Angular.js自定義指令的格式和相關參數與其值:let m=angular.module('angularJS',[]);m.directive('selfDirect',[function(){//selfDirect表示自定義指令的名字,采用駝峰命名法,當restrict的值為E的時候:<self-direct></self-direct>return {restrict:'A/E/C',//A:Attrabute,E:Elements,C:class;restrict屬性表示生成指令在頁面中的表現形式,字母必須大寫,不建議使用C,因為C的寫法與CSS耦合性太強.template:'<p>template選項表示指令在頁面中顯示的內容,template的值可以是字符串也可以是HTML的標簽形式,也可以為函數,如:template:function(elle,attr){return '<span style="'color:'+attr['color']+'">'+ele.html()+'</span>'},view內容太多的時候不建議使用函數的形式</p>',replace:true,//使用模板內容替換包含模板內容的父級標簽transclude:true,//其內容填充到ng-transclude指定的位置templateUrl:'',//不可與template同時使用scope:true,//默認為false,設置指令的作用域,當值為{}時,模板中的變量不會繼承來自控制器中的屬性值,controller:['$scope',function($scope){$scope.data={...}}],//指令中的控制器link:function(scope,elem,attr){},//用link完成對DOM的操作,scope:指令的作用域,elem:指令標簽元素,attr:指令標簽元素的屬性數組,};}])*/var m=angular.module('angularJS',[]);m.directive('selfDirect', [function () {return {restrict: 'E',//template:'<h1><span ng-transclude=""></span>This is a Angular.js direction of self definition</h1><div ng-transclude=""></div>',//replace:true,//transclude:true,//templateUrl:'viewModel.html',//scope:{},//template:'{{title}}<input type="text" ng-model="title">', //template:'<p style="color:{{color}}">suNing store</p><input ng-model="color">',//scope:{color:'@mColor'},//控制器和指令隔離作用域@單項文本綁定,控制器可以影響指令中的數據,而指令不能影響控制器中的數據//scope:{color:'=mColor'},//控制器和指令隔離作用域=雙向文本綁定,控制器可以影響指令中的數據,指令也可以影響控制器中的data//template:'<p>{{logo()}}</p>',//scope:{logo:'&'},//用&符號調用父控制器中的方法/*replace:true,templateUrl:'viewModel.html',controller:['$scope',function($scope){$scope.data=[{id:1,title:'puDong'},{id:2,title:'JinDong'},{id:3,title:'TianMao'}];}],*/scope:{title:'@'},link:function(scope,elem,attr){$(elem).css({backgroundColor:attr['bgcolor'],color:attr['fontcolor']}).html(scope.title);},};}]);/*m.controller('ctrl',['$scope',function($scope){$scope.title='SuNing store';$scope.color='red';$scope.logo=function(){return 'TianMao store';};}]);*/</script></body></html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 浦北县| 彭阳县| 卓尼县| 鹿泉市| 南康市| 徐水县| 柳江县| 河北区| 祁门县| 通山县| 青浦区| 太原市| 宜昌市| 巍山| 晋江市| 尼勒克县| 雷州市| 兴安盟| 江达县| 峡江县| 竹溪县| 缙云县| 旬阳县| 镇沅| 辛集市| 泰兴市| 治多县| 油尖旺区| 澜沧| 大城县| 迁西县| 乌苏市| 科技| 慈利县| 墨竹工卡县| 德化县| 甘洛县| 泽普县| 海门市| 潼关县| 黎平县|