AngularJS實現(xiàn)自定義指令及指令配置項的方法
本文實例講述了AngularJS實現(xiàn)自定義指令及指令配置項的方法。分享給大家供大家參考,具體如下:
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"; }]);
指令配置項
angular.module('myApp', []).directive('first', [ function(){ return { // scope: false, // 默認值,共享父級作用域 // 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, // 繼承父級作用域并創(chuàng)建指令自己的作用域 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment //當修改這里的name時,second會在自己的作用域中新建一個name變量,與父級作用域中的 // name相對獨立,所以再修改父級中的name對second中的name就不會有影響了 template: 'second name:{{name}}', }; }]).directive('third', [ function(){ return { scope: {}, // 創(chuàng)建指令自己的獨立作用域,與父級毫無關系 // 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"; }]);
更多關于AngularJS相關內容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結》、《AngularJS入門與進階教程》及《AngularJS MVC架構總結》
希望本文所述對大家AngularJS程序設計有所幫助。
相關文章
AngularJS ng-controller 指令簡單實例
本文主要介紹AngularJS ng-controller 指令,這里對ng-controller指令資料的整理,并附代碼示例和效果圖,有需要的朋友看下2016-08-08Angular實現(xiàn)較為復雜的表格過濾,刪除功能示例
這篇文章主要介紹了Angular實現(xiàn)較為復雜的表格過濾,刪除功能,結合實例形式分析了AngularJS針對表格的排序、查詢匹配、頁面元素屬性動態(tài)修改等相關操作技巧,需要的朋友可以參考下2017-12-12angular框架實現(xiàn)全選與單選chekbox的自定義
這篇文章主要介紹了angular框架實現(xiàn)全選與單選chekbox的自定義,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07解決nodejs中使用http請求返回值為html時亂碼的問題
下面小編就為大家?guī)硪黄鉀Qnodejs中使用http請求返回值為html時亂碼的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02詳解在Angular4中使用ng2-baidu-map的方法
這篇文章主要介紹了在Angular4中使用ng2-baidu-map的方法,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06AngularJs1.x自定義指令獨立作用域的函數(shù)傳入?yún)?shù)方法
今天小編就為大家分享一篇AngularJs1.x自定義指令獨立作用域的函數(shù)傳入?yún)?shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10