The Wayback Machine - https://web.archive.org/web/20240807033929/https://www.geeksforgeeks.org/angularjs-ng-focus-directive/
Open In App

AngularJS ng-focus Directive

Last Updated : 03 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The ng-focus Directive in AngluarJS is used to apply custom behavior when an element is focused. It can be used to show/hide some element or it can pop up an alert when an element is being focused. It is supported by <a>, <input>, <select> and <textarea> elements.

Syntax:  

<element ng-focus="expression"> 
    Contents... 
</element>

Parameter:

  • expressions: It specifies what to do when the input gets focused.

Example 1: This example uses the ng-focus Directive to display the text area field.  

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>ng-focus Directive</title>
    <script src=
    </script>
</head>
 
<body ng-app="" style="text-align:center">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-focus Directive</h2>
        <a href="" ng-focus="isCheck=true">
        Click to proceed.
    </a><br>
    <div class="row" ng-show="isCheck">
    Enter Name:
        <input type="text"
               ng-focus="isCheck=true"
               placeholder="geeksforgeeks" />
    </div>
</body>
</html>


Output:

 

Example 2: This example uses the ng-focus Directive to display focus on the input text field.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>ng-focus Directive</title>
    <script src=
    </script>
</head>
 
<body ng-app="app" style="text-align:center;">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>ng-focus Directive</h2>
    <div ng-controller="geek"> Input:
        <input type="text"
               ng-focus="focused = true"
               ng-blur="focused = false" />
            <pre ng-show="focused">
                Input is focused.
            </pre>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope',
                        function($scope) {}]);
    </script>
</body>
</html>


Output:

 



Similar Reads

AngularJS ng-show Directive
The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements. Syntax: &lt;element ng-show="expression"&gt; Contents... &lt;/element&gt; Parameter Value: expre
2 min read
AngularJS ng-init Directive
The ng-init Directive is used to initialize AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application. Syntax: &lt;element ng-init = "expression"&gt; ... &lt;/element&gt;Example: This example descr
1 min read
AngularJS ng-if Directive
The ng-if Directive in AngularJS is used to remove or recreate a portion of the HTML element based on an expression. The ng-if is different from the ng-hide directive because it completely removes the element in the DOM rather than just hiding the display of the element. If the expression inside it is false then the element is removed and if it is
2 min read
AngularJS ng-dblclick Directive
The ng-dblclick Directive in AngluarJS is used to apply custom behavior when an element is clicked double. It can be used to show or hide some element or it can popup an alert or change the color of text when it is clicked twice. This means that the element's original ondblclick event will not be overridden by this directive, both will be executed.
2 min read
AngularJS ng-keypress Directive
The ng-keypress Directive in AngluarJS is used to apply custom behavior on a keypress event. It is mainly used to specify what to do when the keyboard is utilized with a particular HTML element. The order of sequence that the key is pressed is Keydown, Keypress, and keyup. It is supported by &lt;input&gt;, &lt;select&gt; and &lt;textarea&gt; elemen
2 min read
AngularJS ng-keydown Directive
The ng-keydown Directive in AngluarJS is used to apply custom behavior on a keydown event. This directive will not be overridden by the element's original onkeydown event. Both the onkeydown event &amp; the ng-keydown Directive will be executed. It is supported by &lt;input&gt;, &lt;select&gt; and &lt;textarea&gt; elements. Syntax: &lt;element ng-k
2 min read
AngularJS ng-keyup Directive
The ng-keyup Directive in AngluarJS is used to apply custom behavior on a keyup event. It is supported by &lt;input&gt;, &lt;select&gt; and &lt;textarea&gt; elements. Syntax: &lt;element ng-keyup="expression"&gt; Contents... &lt;/element&gt; Parameter: expression: When a keypress is finished, then the followed expression will be executed.Example 1:
2 min read
AngularJS ng-href Directive
The ng-href directive is used when we have an angular expression inside the href value. If href attribute is used then the problem is that if in case the link is clicked before AngularJS has replaced the expression with its value then it may go to the wrong URL and the link will be broken and will most likely return a 404 error while ng-href direct
2 min read
AngularJS ng-hide Directive
The ng-hide Directive in AngluarJS is used to show or hide the specified HTML element. If the expression given in the ng-hide attribute is true then the HTML elements hide. In AngularJS there is a predefined class named ng-hide which is used to set the display to none. Syntax: &lt;element ng-hide="expression"&gt; Contents... &lt;/element&gt; Parame
2 min read
AngularJS ng-disabled Directive
In this article, we will see the AngularJS ng-disabled Directive, along with understanding its implementation through the illustrations. The ng-disabled Directive in AngularJS is used to enable or disable the HTML elements. If the expression inside the ng-disabled attribute returns true then the form field will be disabled or vice versa. It is usua
2 min read
three90RightbarBannerImg