AngularJS Directive的使用

(1)使用Directive自定义HTML组件

restrict

replace

template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AngularJS $http</title>

    <link rel="stylesheet" href="css/foundation.min.css">
</head>
<body>
    <div ng-app="app">
        <!--<hello></hello>-->
        <!--<div hello></div>-->
        <div class="hello"></div>
        <div class="geek"></div>
    </div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>

var app = angular.module('app',[]);

app.directive('hello',function () {
    return {
        /*restrict:'E',
        replace:true,//替换掉directive自定义的名称
        template:'<div>Hello World</div>'*/

        /*restrict:'A',
        link:function () {
            alert("我在这里");
        }*/

        restrict:'C',
        link:function () {
            alert("我在这里");
        }
    }
})

app.directive('geek',function () {
    return {
        restrict:'C',
        link:function () {
            alert("我在这里geek");
        }
    }
})

(2)Directive和Controller之间的会话

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AngularJS $http</title>

    <link rel="stylesheet" href="css/foundation.min.css">
</head>
<body>
    <div ng-app="app">
        <div ng-controller="AppCtrl">
            <div enter="loadMoreData()">I'm here</div>
        </div>
    </div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>

var app = angular.module('app',[])

app.controller('AppCtrl',function ($scope) {
    $scope.loadMoreData = function () {
        alert("doing...");
    }
})

app.directive('enter',function () {
    return {
        restrict:'A',//默认也是A
        link:function (scope,element,attrs) {
            element.bind('mouseenter',function () {
                scope.$apply(attrs.enter);
            })
        }
    }
})

----------------------------------------

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AngularJS $http</title>

    <link rel="stylesheet" href="css/foundation.min.css">
</head>
<body>
    <div ng-app="app">
        <food apple orange banana>所有食物</food><br/>
        <food apple orange>所有食物</food>
    </div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>

var app = angular.module('app',[])

app.directive('food',function () {
    return {
        restrict:'E',
        scope:{},
        controller:function ($scope) {
            $scope.foods=[];
            this.addApple = function () {
                $scope.foods.push("apple");
            }
            this.addOrange = function () {
                $scope.foods.push("orange");
            }
            this.addBanana = function () {
                $scope.foods.push("banana");
            }
        },
        link:function (scope,element,attrs) {
            element.bind('mouseenter',function () {
                console.log(scope.foods);
            });
        }
    }
})

app.directive('apple',function () {
    return {
        require:'food',
        link:function (scope,element,attrs,foodCtrl) {
            foodCtrl.addApple();
        }
    }
})

app.directive('orange',function () {
    return {
        require:'food',
        link:function (scope,element,attrs,foodCtrl) {
            foodCtrl.addOrange();
        }
    }
})

app.directive('banana',function () {
    return {
        require:'food',
        link:function (scope,element,attrs,foodCtrl) {
            foodCtrl.addBanana();
        }
    }
})

(3)使用angular.element操作Dom

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AngularJS $http</title>

    <link rel="stylesheet" href="css/foundation.min.css">
</head>
<body>
    <div ng-app="app">
        <!--<div enter leave>I'm here</div>-->
        <hello></hello>
    </div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>

var app = angular.module('app',[])

app.directive('enter',function () {
    return function (scope,element,attrs) {
        console.log(element);
        element.bind('mouseenter',function () {
            element.addClass("alert-box");
        })
    }
})

app.directive('leave',function () {
    return function (scope,element,attrs) {
        console.log(element);
        element.bind('mouseleave',function () {
            element.removeClass("alert-box");
        })
    }
})

app.directive('hello',function () {
    return {
        restrict:'E',
        template:'<div><input ng-model="txt"></div><div>{{txt}}</div>',
        link:function (scope,element) {
            scope.$watch('txt',function (newVal) {
                if(newVal === 'error'){
                    element.addClass('alert-box alert');
                }
            })
        }
    }
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值