400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Angular中Directive怎么用

这篇文章主要介绍了Angular中Directive怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联专注于如东网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供如东营销型网站建设,如东网站制作、如东网页设计、如东网站官网定制、微信小程序开发服务,打造如东网络公司原创品牌,更为您提供如东网站排名全网营销落地服务。

Angular Directive 学习

学习目的:为了更好的了解 ng directive 的使用方法。

Directive可能是AngularJS中比较复杂的一个东西了。一般我们将其理解成指令。AngularJS自带了不少预设的指令,比如ng-app,ng-controller这些。可以发现个特点,AngularJS自带的指令都是由ng-打头的。

那么,Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html、js封装在一起,形成一个可复用的独立个体,具体特定的功能。下面我们来详细解读一下Directive的一般性用法。

AnguarJS directive的常用定义格式以及参数说明

看下面的代码:
var myDirective = angular.module('directives', []);

myDirective.directive('directiveName', function($inject) {
    return {
        template: '
',         replace: false,         transclude: true,         restrict: 'E',         scope: {},         controller: function($scope, $element) {         },         complie: function(tElement, tAttrs, transclude) {             return {                 pre: function preLink(scope, iElement, iAttrs, controller) {                 },                 post: function postLink(scope, iElement, iAttrs, controller) {                 }             };         },         link: function(scope, iElement, iAttrs) {         }     }; });
return {
    name: '',
    priority: 0,
    terminal: true,
    scope: {},
    controller: fn,
    require: fn,
    restrict: '',
    template: '',
    templateUrl: '',
    replace: '',
    transclude: true,
    compile: fn,
    link: fn
}

相关教程推荐:《angular教程》

如上所示,return的对象中会有很多的属性,这行属性都是用来定义directive的。

下面我们来一个个的说明他们的作用。

关于scope

这里关于directive的scope为一个object时,有更多的内容非常有必要说明一下。看下面的代码:

scope: {
    name: '=',
    age: '=',
    sex: '@',
    say: '&'
}

这个scope中关于各种属性的配置出现了一些奇怪的前缀符号,有=,@,&,那么这些符号具体的含义是什么呢?再看下面的代码:

复制代码
function Controller($scope) {
    $scope.name = 'Pajjket';
    $scope.age = 99;
    $scope.sex = '我是男的';
    $scope.say = function() {
        alert('Hello,我是弹出消息');
    };
}
可以看出,几种修饰前缀符的大概含义:

在本地scope属性与parent scope属性之间设置双向的绑定。如果没有指定attr名称,那么本地名称将与属性名称一致。

,widget的scope定义为:{localFn:’increment()’},那么isolate scope property localFn会指向一个包裹着increment()表达式的function。 一般来说,我们希望通过一个表达式,将数据从isolate scope传到parent scope中。这可以通过传送一个本地变量键值的映射到表达式的wrapper函数中来完成。例如,如果表达式是increment(amount),那么我们可以通过localFn({amount:22})的方式调用localFn以指定amount的值。

directive 实例讲解

下面的示例都围绕着上面所作的参数说明而展开的。

// 自定义directive
var myDirective = angular.modeule('directives', []);

myDirective.directive('myTest', function() {
    return {
        restrict: 'EMAC',
        require: '^ngModel',
        scope: {
            ngModel: '='
        },
        template: '
Weather for {{ngModel}}'     }; }); // 定义controller var myControllers = angular.module('controllers', []); myControllers.controller('testController', [     '$scope',     function($scope) {         $scope.name = 'this is directive1';     } ]); var app = angular.module('testApp', [     'directives',     'controllers' ]);                                              

template与templateUrl的区别和联系

templateUrl其实根template功能是一样的,只不过templateUrl加载一个html文件,上例中,我们也能发现问题,template后面根的是html的标签,如果标签很多呢,那就比较不爽了。可以将上例中的,template改一下。

myDirective.directive('myTest', function() {
    return {
        restrict: 'EMAC',
        require: '^ngModel',
        scope: {
            ngModel: '='
        },
        templateUrl:'../partials/tem1.html'   //tem1.html中的内容就是上例中template的内容。
    }
});
scope重定义
//directives.js中定义myAttr
myDirectives.directive('myAttr', function() {
    return {
        restrict: 'E',
        scope: {
            customerInfo: '=info'
        },
        template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}
' +                   'Name: {{vojta.name}} Address: {{vojta.address}}'     }; }); //controller.js中定义attrtest myControllers.controller('attrtest',['$scope',     function($scope) {         $scope.naomi = {             name: 'Naomi',             address: '1600 Amphitheatre'         };         $scope.vojta = {             name: 'Vojta',             address: '3456 Somewhere Else'         };     } ]); // html中                   
其运行结果如下:
Name: Naomi Address: 1600 Amphitheatre      //有值,因为customerInfo定义过的
Name: Address:                              //没值 ,因为scope重定义后,vojta是没有定义的
我们将上面的directive简单的改一下,
myDirectives.directive('myAttr', function() {
    return {
        restrict: 'E',
        template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}
' +                   'Name: {{vojta.name}} Address: {{vojta.address}}'     }; });
Name: Address:
Name: Vojta Address: 3456 Somewhere Else

因为此时的directive没有定义独立的scope,customerInfo是undefined,所以结果正好与上面相反。

transclude的使用

myDirective.directive('myEvent', function() {
    return {
        restrict: 'E',
        transclude: true,
        scope: {
            'close': '$onClick'      //根html中的on-click="hideDialog()"有关联关系
        },
        templateUrl: '../partials/event_part.html'
    };
});

myController.controller('eventTest', [
    '$scope',
    '$timeout',
    function($scope, $timeout) {
        $scope.name = 'Tobias';
        $scope.hideDialog = function() {
            $scope.dialogIsHidden = true;
            $timeout(function() {
                $scope.dialogIsHidden = false;
            }, 2000);
        };
    }
]);

    
        
            Check out the contents, {{name}}!
        
    



    ×     

    
        
            Check out the contents, {{name}}!
        
    
myDirective.directive('exampleDirective', function() {
    return {
        restrict: 'E',
        template: '

Hello {{number}}!

',         controller: function($scope, $element){             $scope.number = $scope.number + "22222 ";         },         link: function(scope, el, attr) {             scope.number = scope.number + "33333 ";         },         compile: function(element, attributes) {             return {                 pre: function preLink(scope, element, attributes) {                     scope.number = scope.number + "44444 ";                 },                 post: function postLink(scope, element, attributes) {                     scope.number = scope.number + "55555 ";                 }             };         }     } }); //controller.js添加 myController.controller('directive2',[     '$scope',     function($scope) {         $scope.number = '1111 ';     } ]); //html                   
Hello 1111 22222 44444 5555 !

由结果可以看出来,controller先运行,compile后运行,link不运行。 我们现在将compile属性注释掉后,得到的运行结果如下:Hello 1111 22222 33333 !

由结果可以看出来,controller先运行,link后运行,link和compile不兼容。一般地,compile比link的优先级要高。

感谢你能够认真阅读完这篇文章,希望小编分享的“Angular中Directive怎么用”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


分享题目:Angular中Directive怎么用
标题URL:http://mbwzsj.com/article/jjesjh.html

其他资讯

让你的专属顾问为你服务