UrbanPro
true

Learn Angular.JS from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Angular JS directives Principles.

Rupesh Kumar
16/01/2017 0 0

Directive is very powerful feature of AngularJS. It easily wired up with controller, html and do the DOM manipulations. Angular AngularJS provide very less details about directive. I thought I will cover some of the directive that I used in real projects.

Compile Function :

use for template DOM manipulation (i.e., manipulation of tElement = template element), hence manipulations that apply to all DOM clones of the template associated with the directive.

Example: disable all element in a DIV

  1. app.directive('disableContents', function () {
  2. return {
  3. compile: function (tElem, tAttrs) {
  4. var inputs = tElem.find('input');
  5. inputs.attr('ng-disabled',tAttrs['disableContents']);
  6. for(var i = 0; i < inputs.length; i++) {
  7. }
  8. }
  9. }
  10. });

Link Function:

Use for registering DOM listeners (i.e., $watch expressions on the instance scope) as well as instance DOM manipulation (i.e., manipulationof iElement = individual instance element).

Check Valid email on textbox blur

  1. app.directive('validEmail', ['$dependency', function ($dependency) {
  2. return {
  3. require: 'ngModel',
  4. link: function (scope, element, attrs, ngModel) {
  5. element.bind('blur', function (e) {
  6. if (!ngModel || !element.val()) return;
  7. var currentValue = element.val();
  8. var regex = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
  9. if (currentValue && !regex.test(currentValue)) {
  10. ngModel.$modelValue = '';
  11. element[0].value = '';
  12. alert(INVALID EMAIL);
  13. }
  14. });
  15. }
  16. }
  17. }]);

You can use directive in all scenario where you want to manipulate DOM elements.

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Pro Developer Program Syllabus
What you will learn in Pro-Developer Program: i. Introduction to Web Technologies. ii. Introduction to .Net - Features of .Net, CTS, CLS, CLR and MSIL. iii. C# & .Net Basics - Data Types,...

NgModules vs. JavaScript modules (Angular 2)
The NgModule — a class decorated with @NgModule Decorator — is a fundamental feature of Angular. JavaScript also has its own module system for managing collections of JavaScript objects. It's...

Angular 4 Forms: Template Driven Approach
Angular Forms: Forms in Angular 4 can be designed on two approaches. As you can see below , we have a Template based design and the other being a Reactive based design. Here we will try to understand...

Remote Learning Do's and Dont's
In the Present fast-paced world, people are facing challenges concerning keeping themselves always with the up to date technologies or industry changes. When it comes to the IT industry, it is becoming...
V

Angular Js Workshop & Courses
AngularJS is an open source JavaScript framework by Google to build web applications. It can be freely used, changed and shared by anyone. Prerequisite: Before learning AngularJS, you must...
X

Looking for Angular.JS Classes?

The best tutors for Angular.JS Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Angular.JS with the Best Tutors

The best Tutors for Angular.JS Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more