Showing posts with label directives in angularjs. Show all posts
Showing posts with label directives in angularjs. Show all posts

AngularJS Blur Directive

There is no direct support for blur event in AngularJs. But we can add support for blur event by creating directive.

Add the following directive to your javascript file

app.directive('ngBlur', ['$parse', function($parse) {
  return function(scope, element, attr) {
    var fn = $parse(attr['ngBlur']);
    element.bind('blur', function(event) {
      scope.$apply(function() {
        fn(scope, {$event:event});
      });
    });
  }
}]);

Then use it in your html template like below

<input type="text" ng-blur="myFunc()"/>

And write the function myFunc() in your controller

$scope.myFunc = function(){
   alert("hello!!");
}

Hope this helps you.

For more posts on AngularJs visit: http://coding-issues.blogspot.in/search/label/angularJS

Read more...

Directive Restrictions in AngularJS

In AngularJS, while creating a custom directive, we can have different kinds of restrictions depending on how we are going use that directive.

The different kinds of Restrictions are:

1. A - Attribute (Note : A is the Default Restriction)
2. C - Class
3. E - Element
4. M - Comment

Example:

1. A- Attribute :

If you want to use your directive as attribute of any element then you have to write the restriction as 'A'.

Javascript:

        var myApp = angular.module('myApp', []);

        myApp.directive('myDirective', function () {

            return {

                restrict: 'A',

                link: function () {

                    alert("A is working..");

                }

            }

        })


Html  :

<html ng-app="myApp">

<head>

<title>AngularJS Directives</title>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>

    <script src="myAngular.js"></script>

</head>

<body>

    <div myDirective></div>

</body>

</html>


Note: We are using our directive as Attribute for "div" element.

2. C - Class :

If you want to use your directive as class then you have to write the restriction as 'E'.


Javascript:

        var myApp = angular.module('myApp', []);



        myApp.directive('myDirective', function () {

            return {

                restrict: 'C',

                link: function () {

                    alert("c is working..");

                }

            }           

        })


Html  :

<html ng-app="myApp">

<head>

<title>AngularJS Directives</title>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>

    <script src="myAngular.js"></script>

</head>

<body>

    <div class="myDirective"></div>

</body>

</html>


Note: Here we are using our directive as a class.

3. E - Element :

If you want to use your directive as html element then you have to write the restriction as 'E'.

Javascript:

        var myApp = angular.module('myApp', []);



        myApp.directive('myDirective', function () {

            return {

                restrict: 'E',

                template:'<div>This text is from custom directive..</div>'

            }                       

        })


Html :

<html ng-app="myApp">

<head>

<title>AngularJS Directives</title>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>

    <script src="myAngular.js"></script>

</head>

<body>

    <myDirective></myDirective>

</body>

</html>


Note: Here we are using our directive as element.

4. M - Comment :

If you want to use your directive as comment then you have to write the restriction as 'M'.

Javascript :

        var myApp = angular.module('myApp', []);



        myApp.directive('myDirective', function () {

            return {

                restrict: 'M',

                link: function () {

                    alert('M is working..');

                }

            }                                   

        })


Html :

<html ng-app="myApp">

<head>

<title>AngularJS Directives</title>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>

    <script src="myAngular.js"></script>

</head>

<body>

    <!-- directive:myDirective -->

    <div></div></body>

</html>


Note: Here you are invoking your directive using a comment(careful with the spaces in comment. If comment is not properly formed, you may not get the result).

These are the different kinds of directives restrictions in AngularJS.

For more posts on angularJS refer: AngularJS

Read more...