[AngularJS] How to create Directives?

时间:2014-01-20 22:49:30   收藏:0   阅读:416

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);
            }
        }
    };
});


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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!