這篇文章將為大家詳細(xì)講解有關(guān)AngularJS怎么實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司:2013年至今為各行業(yè)開拓出企業(yè)自己的“網(wǎng)站建設(shè)”服務(wù),為近1000家公司企業(yè)提供了專業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)和網(wǎng)站推廣服務(wù), 定制網(wǎng)站建設(shè)由設(shè)計(jì)師親自精心設(shè)計(jì),設(shè)計(jì)的效果完全按照客戶的要求,并適當(dāng)?shù)奶岢龊侠淼慕ㄗh,擁有的視覺效果,策劃師分析客戶的同行競爭對手,根據(jù)客戶的實(shí)際情況給出合理的網(wǎng)站構(gòu)架,制作客戶同行業(yè)具有領(lǐng)先地位的。
1、能夠嵌入動(dòng)態(tài)文本于HTML頁面。2、對瀏覽器事件做出響應(yīng)。3、讀寫HTML元素。4、在數(shù)據(jù)被提交到服務(wù)器之前驗(yàn)證數(shù)據(jù)。5、檢測訪客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。7、基于Node.js技術(shù)進(jìn)行服務(wù)器端編程。
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)值,共享父級作用域 // 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 //當(dāng)修改這里的name時(shí),second會(huì)在自己的作用域中新建一個(gè)name變量,與父級作用域中的 // name相對獨(dú)立,所以再修改父級中的name對second中的name就不會(huì)有影響了 template: 'second name:{{name}}', }; }]).directive('third', [ function(){ return { scope: {}, // 創(chuàng)建指令自己的獨(dú)立作用域,與父級毫無關(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怎么實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。