AngularJS Events

1) ng-click

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4880246846967300&output=html&h=60&slotname=6239393533&adk=202425667&adf=2306573245&pi=t.ma~as.6239393533&w=468&lmt=1621006853&psa=1&format=468×60&url=https%3A%2F%2Fscripts.guru%2Fangularjs-events%2F%3Futm_source%3Drss%26utm_medium%3Drss%26utm_campaign%3Dangularjs-events&flash=0&wgl=1&adsid=ChEI8L_4hAYQrb-iq9CI64XZARI9AOMPUgO2a5vTcLVAB7CJT9voq_O-r-SzYcUPuAop7x9th-n9rpHFoqFq1gjOQo-OduC_Xqb-G8FDg7XuIw&uach=WyJXaW5kb3dzIiwiMTAuMCIsIng4NiIsIiIsIjkwLjAuNDQzMC4yMTIiLFtdXQ..&dt=1621006851033&bpp=3&bdt=6636&idt=1163&shv=r20210511&cbv=%2Fr20190131&ptt=9&saldr=aa&abxe=1&cookie=ID%3Dae88d6ef60dfeb51-220624c824c80065%3AT%3D1620881451%3ART%3D1620881451%3AS%3DALNI_MbziyJ-Hxj_TMPSW5tsSHH0KW2DEA&prev_fmts=728×90%2C300x250%2C0x0&nras=1&correlator=1735026327341&frm=20&pv=1&ga_vid=1477556874.1620554064&ga_sid=1621006852&ga_hid=1464170960&ga_fc=0&u_tz=330&u_his=1&u_java=0&u_h=864&u_w=1536&u_ah=864&u_aw=1536&u_cd=24&u_nplug=3&u_nmime=4&adx=340&ady=1033&biw=1519&bih=735&scr_x=0&scr_y=0&eid=31060980&oid=3&pvsid=1558493135739161&pem=494&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1536%2C0%2C1536%2C864%2C1536%2C735&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&jar=2021-05-14-15&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=5rKSCALJ5n&p=https%3A//scripts.guru&dtd=2240

<!DOCTYPE html>
<html>
<head>
    <meta chrset="UTF 8">
    <title>Event</title>
</head>
<body ng-app="">

<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<h1> Script guru Global Event</h1>

<button ng-click="count = count + 1" ng-init="count=0">
    Increment
</button>

<div>The Current Count is {{count}}</div>

</body>
</html>
  • We are introducing the ng-click event directive to the button. In this directive, we are writing code to increment the value of the count variable by 1.
  • Here we are displaying the value of the count variable to the user.

2) ng-show

<!DOCTYPE html>
<html>
<head>
    <meta chrset="UTF 8">
    <title>Event</title>
</head>
<body>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<h1> Script guru Event</h1>
<div ng-app="DemoApp" ng-controller="DemoController">
    <input type="button" value="Show Angular" ng-click="ShowHide()"/>

    <br><br><div ng-show = "IsVisible">Angular</div>
</div>

<script type="text/javascript">

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

    app.controller('DemoController',function($scope){
        $scope.IsVisible = false;

        $scope.ShowHide = function(){
            $scope.IsVisible = $scope.IsVisible = true;
        }
        });
</script>

</body>
</html>
  • We are attaching the ng-click event directive to the button element. Over here we are referencing a function called “ShowHide” which is defined in our controller – DemoController.
  • We are attaching the ng-show attribute to a div tag which contains the text Angular. This is the tag which we are going to show/hide based on the ng-show attribute.
  • In the controller, we are attaching the “IsVisible” member variable to the scope object. This attribute will be passed to the ng-show angular attribute (step#2) to control the visibility of the div control. We are initially setting this to false so that when the page is first displayed the div tag will be hidden.Note:- When the attributes ng-show is set to true, the subsequent control which in our case is the div tag will be shown to the user. When the ng-show attribute is set to false the control will be hidden from the user.
  • We are adding code to the ShowHide function which will set the IsVisible member variable to true so that the div tag can be shown to the user.

3) ng-hide

<!DOCTYPE html>
<html>
<head>
    <meta chrset="UTF 8">
    <title>Event</title>
</head>
<body>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<h1> Script guru Global Event</h1>
<div ng-app="DemoApp" ng-controller="DemoController">
    <input type="button" value="Hide Angular" ng-click="ShowHide()"/>

    <br><br><div ng-hide="IsVisible">Angular</div>
</div>

<script type="text/javascript">

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

    app.controller('DemoController',function($scope){
        $scope.IsVisible = false;

        $scope.ShowHide = function(){
            $scope.IsVisible = $scope.IsVisible = true;
        }
        });
</script>

</body>
</html>
  • We are attaching the ng-click event directive to the button element. Over here we are referencing a function called ShowHide which is defined in our controller – DemoController.
  • We are attaching the ng-hide attribute to a div tag which contains the text Angular. This is the tag, which we are going to shotw/hide based on the ng-show attribute.
  • In the controller, we are attaching the isVisible member variable to the scope object. This attribute will be passed to the ng-show angular attribute to control the visibility of the div control. We are initially setting this to false so that when the page is first displayed the div tag will be hidden.
  • We are adding code to the ShowHide function which will set the IsVisible member variable to true so that the div tag can be shown to the user.

Leave a Reply

Your email address will not be published. Required fields are marked *