coder
coder

Reputation: 1

Why AngularJS directives are preferred?

DOM Manipulation and data binding can be done with Jquery also. What is the advantage of going with AngularJS Directives?

Upvotes: 0

Views: 55

Answers (2)

balajivaishnav
balajivaishnav

Reputation: 2893

In the simplest terms, a directive in AngularJS is a reusable component. Directives in AngularJS encapsulate all the behavioral properties and functionalities of an element in a semantic way, thereby keeping all of the functionality grouped together. This helps to keep track of changes of one HTML section in one place rather than tracking the changes on a global level in a script.

A more formal definition of a directive is: In AngularJS, a directive is a JavaScript factory function defined inside an AngularJS module that holds a set of instructions for the HTML compiler for defining a specified behavior of a DOM element.

A sample Directive Code

var myApp = angular.module(‘myApp’, []);
 myApp.directive(‘helloWorld’, function() {
    return {
        restrict: ‘E’,
template: ‘<h1>Hello World</h1>’
    };
});

<body ng-app=“myApp”>
    <hello-world></hello-world>
</body>

To know more about directive click the link

Upvotes: 1

Bharat
Bharat

Reputation: 2462

Angular js is two way binding so it will reduce writing more code. if any instance is changed then it will reflect everywhere in page

Upvotes: 0

Related Questions