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中创建自定义指令,并通过实例演示了两种不同类型的指令:hi和hello。此外,还展示了如何使用$compile服务动态添加这些指令到DOM中。
1744

被折叠的 条评论
为什么被折叠?



