首页 > Web开发 > 详细

AngulatJS $directive

时间:2014-04-20 11:12:42      阅读:399      评论:0      收藏:0      [点我收藏+]

restrict : E/A/C/M 绑定元素的类型 E element A attribute C class M comment

template: 返回的string

replace :是否去掉外层wrap

controller 内置的控制器

简单例子:

bubuko.com,布布扣
<!DOCTYPE html>

<html ng-app="demo">
<head>
    <title></title>
    <script src="angular.min.js" type="text/javascript"></script>
</head>
<body>
    <div my-Directive></div>
</body>
<script>
    var demo = angular.module("demo", []);
    demo.controller("demoController", function ($scope) {

    });
    demo.directive("myDirective", function () {
        return {
            restrict: "A",
            replace: true,
            controller: function ($scope) {
                $scope.name = "click me";
            },
            template: "<a href=‘http://google.com‘>{{name}}</a>"
        };
    });
</script>
</html>
bubuko.com,布布扣

 directive 与DOM交互的理解可以通过scope来传递

bubuko.com,布布扣
<!DOCTYPE html>

<html ng-app="demo">
<head>
    <title></title>
    <script src="angular.min.js" type="text/javascript"></script>
</head>
<body>
    <div my-Directive my-Url="http://google.com" my-Text="click me"></div>
</body>
<script>
    var demo = angular.module("demo", []);
    demo.controller("demoController", function ($scope) {

    });
    demo.directive("myDirective", function () {
        return {
            restrict: "A",
            replace: true,
            controller: function ($scope) {
                //$scope.name = "click meee";
            },
            scope:{
                myUrl:"@",
                myText:"@"
            },
            template: "<a href=‘{{myUrl}}‘>{{myText}}</a>"
        };
    });
</script>
</html>
bubuko.com,布布扣

 

 

 再看另外一个例子

bubuko.com,布布扣
<!DOCTYPE html>

<html ng-app="demo">
<head>
    <title></title>
    <script src="angular.min.js" type="text/javascript"></script>
</head>
<body>
    <input type="text" ng-model="theirUrl" />
    <div my-Directive my-Url="http://google.com" my-Text="click me" some-Attr="theirUrl"></div>
</body>
<script>
    var demo = angular.module("demo", []);
    demo.controller("demoController", function ($scope) {

    });
    demo.directive("myDirective", function () {
        return {
            restrict: "A",
            replace: true,
            controller: function ($scope) {
                //$scope.name = "click meee";
            },
            scope:{
                myUrl:"=someAttr",//modified
                myText:"@"
            },
            template: "<a href=‘{{myUrl}}‘>{{myText}}</a>"
        };
    });
</script>
</html>
bubuko.com,布布扣

用theirUrl将some-Attr关联起来,通过scope传递值进去directive

AngulatJS $directive,布布扣,bubuko.com

AngulatJS $directive

原文:http://www.cnblogs.com/lihaozhou/p/3675474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!