To extend the functionality of HTML in AngularJS, custom directives are used using “directive” function. This simply replaces the element for which this is activated.
Using its compile() method AngularJS application finds the matching elements and process for one time activity.
For the following types of elements AngularJS provides support to create custom directives.
– Element directives
– Attribute
– CSS
– Comment
<html>
<head>
<title>AngularJS custom directives</title>
</head>
<body>
<h2>AngularJS custom directives</h2>
<div ng-app="myApp" ng-controller="UserController">
<user name="John"></user>
<user name="Jane"></user>
<user name="Steve"></user>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var myApp = angular.module("myApp", []);
myApp.directive('user', function() {
var directive = {};
directive.restrict = 'E';
directive.template = "Name: <b>{{user.name}}</b>, Age: <b>{{user.age}}</b>";
directive.scope = {
user : "=name"
}
directive.compile = function(element, attributes) {
element.css("border", "1px solid #ffffff");
var linkFunction = function($scope, element, attributes) {
element.html("Name: <b>"+$scope.user.name +"</b>, Age: <b>"+$scope.user.age+"</b><br/>");
element.css("background-color", "#c2c2c2");
}
return linkFunction;
}
return directive;
});
myApp.controller('UserController', function($scope) {
$scope.John = {};
$scope.John.name = "John Doe";
$scope.John.age = 47;
$scope.Jane = {};
$scope.Jane.name = "Jane Doe";
$scope.Jane.age = 33;
$scope.Steve = {};
$scope.Steve.name = "Steve Martin";
$scope.Steve.age = 25;
});
</script>
</body>
</html> Let’s face it. Tech buzzwords get thrown around a lot—especially when it comes to how…
In today’s digital world, the boundaries between technology, finance, and innovation are rapidly disappearing. Businesses…
Backyard gatherings like BBQs, family reunions, and garden parties are an exciting way to enjoy…
Marketers are always on the lookout for more effective ways to reach their target audiences.…
Does your phone control your mind more than you control your phone? Modern life exploits…
Did you know that the prostate continues growing throughout a man's entire life, making BPH…
View Comments
This is nice. Thanks for sharing this.
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
Very interesting, good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up.
Thank you so much for sharing your knowledge.
Thank you for the excellent information you have shared with us, Please keep us update on new technologies