AngularJS實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法
本文實(shí)例講述了AngularJS實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法。分享給大家供大家參考,具體如下:
AngularJS自定義指令有兩種寫法:
//第一種 angular.module('MyApp',[]) .directive('zl1',zl1) .controller('con1',['$scope',func1]); function zl1(){ var directive={ restrict:'AEC', template:'this is the it-first directive', }; return directive; }; function func1($scope){ $scope.name="alice"; } //第二種 angular.module('myApp',[]).directive('zl1',[ function(){ return { restrict:'AE', template:'thirective', link:function($scope,elm,attr,controller){ console.log("這是link"); }, controller:function($scope,$element,$attrs){ console.log("這是con"); } }; }]).controller('Con1',['$scope',function($scope){ $scope.name="aliceqqq"; }]);
指令配置項(xiàng)
angular.module('myApp', []).directive('first', [ function(){ return { // scope: false, // 默認(rèn)值,共享父級(jí)作用域 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment template: 'first name:{{name}}', }; }]).directive('second', [ function(){ return { scope: true, // 繼承父級(jí)作用域并創(chuàng)建指令自己的作用域 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment //當(dāng)修改這里的name時(shí),second會(huì)在自己的作用域中新建一個(gè)name變量,與父級(jí)作用域中的 // name相對(duì)獨(dú)立,所以再修改父級(jí)中的name對(duì)second中的name就不會(huì)有影響了 template: 'second name:{{name}}', }; }]).directive('third', [ function(){ return { scope: {}, // 創(chuàng)建指令自己的獨(dú)立作用域,與父級(jí)毫無關(guān)系 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment template: 'third name:{{name}}', }; }]) .controller('DirectiveController', ['$scope', function($scope){ $scope.name="mike"; }]);
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
- 深入講解AngularJS中的自定義指令的使用
- AngularJS創(chuàng)建自定義指令的方法詳解
- AngularJS優(yōu)雅的自定義指令
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS 自定義指令詳解及實(shí)例代碼
- AngularJS自定義指令實(shí)現(xiàn)面包屑功能完整實(shí)例
- AngularJS實(shí)現(xiàn)自定義指令與控制器數(shù)據(jù)交互的方法示例
- AngularJS 自定義指令詳解及示例代碼
- AngularJS自定義指令之復(fù)制指令實(shí)現(xiàn)方法
- AngularJS自定義指令詳解(有分頁插件代碼)
- 詳解angularJS自定義指令間的相互交互
相關(guān)文章
AngularJS ng-controller 指令簡(jiǎn)單實(shí)例
本文主要介紹AngularJS ng-controller 指令,這里對(duì)ng-controller指令資料的整理,并附代碼示例和效果圖,有需要的朋友看下2016-08-08Angular實(shí)現(xiàn)較為復(fù)雜的表格過濾,刪除功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)較為復(fù)雜的表格過濾,刪除功能,結(jié)合實(shí)例形式分析了AngularJS針對(duì)表格的排序、查詢匹配、頁面元素屬性動(dòng)態(tài)修改等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12angular框架實(shí)現(xiàn)全選與單選chekbox的自定義
這篇文章主要介紹了angular框架實(shí)現(xiàn)全選與單選chekbox的自定義,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07解決nodejs中使用http請(qǐng)求返回值為html時(shí)亂碼的問題
下面小編就為大家?guī)硪黄鉀Qnodejs中使用http請(qǐng)求返回值為html時(shí)亂碼的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02詳解在Angular4中使用ng2-baidu-map的方法
這篇文章主要介紹了在Angular4中使用ng2-baidu-map的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06AngularJs1.x自定義指令獨(dú)立作用域的函數(shù)傳入?yún)?shù)方法
今天小編就為大家分享一篇AngularJs1.x自定義指令獨(dú)立作用域的函數(shù)傳入?yún)?shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10