Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Angular Js , Some Useful Directives

0.00/5 (No votes)
22 Feb 2016 3  
Some most common useful directives

Introduction

In Web development section mostly we required some usefull things as these directive help us a lot

ngEnter

for Enter functionality on page

ngFocusWith

Focus a input field on expression basis

 

 

'use strict';


angular.module('myApp').directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 13) {
                scope.$apply(function () {
                    scope.$eval(attrs.ngEnter);
                });

                event.preventDefault();
            }
        });
    };
});
 angular.module('myApp').directive('ngFocusWith', function ($timeout) {
    return {
        restrict: 'A',
        scope: {
            focusValue: "=ngFocusWith"
        },
        link: function ($scope, $element, attrs) {
            $scope.$watch("focusValue", function (currentValue, previousValue) {
                if (currentValue === true && !previousValue) {
                    // Manage $digest on watch triggering
                    $timeout(function () {
                        $element[0].focus();
                        $element[0].select();
                    }, 0, false);
                } else if (currentValue === false && previousValue) {
                    // Manage $digest on watch triggering
                    $timeout(function () {
                        $element[0].blur();
                    }, 0, false);
                }
            });
        }
    }
});

These Directive are commany used and not easily available

Points of Interest

Helping contenets in case of angular js

History

a lot of efforts are added to complete this

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here