首页 > Web开发 > 详细

[AngularJS] How to create Directives?

时间:2014-01-20 22:49:30      阅读:417      评论:0      收藏:0      [点我收藏+]

Directives are the reusable UI components in AngularJS. In this blog, i will show you how to create custom directives in AngularJS.

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

app.directive(‘hi‘, function() {
    return {
        template: ‘<h2>Hi There</h2>‘,
        replace: true
    };
});

app.directive(‘hello‘, function() {
    return {
        template: ‘<h2>Hello There</h2>‘,
        replace: true
    };
});

app.directive(‘customDirective‘, function($compile) {
    return {
        template: ‘<a ng-click="addType(\‘hi\‘)">Add Hi</a><br/><a ng-click="addType(\‘hello\‘)">Add Hello</a><div class="holder">‘,
        link: function(scope, element, attr) {
            scope.addType = function(type) {
                var el = $compile(‘<div ‘ + type + ‘></div>‘)(scope);
                $(‘.holder‘, element).append(el);
            }
        }
    };
});


[AngularJS] How to create Directives?

原文:http://blog.csdn.net/wonderfan/article/details/18497109

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