Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Angularjs Show JavaScript Alert Message (Box) on ng-click with Example

May 6, 2015
Introduction

Here I will explain how to show alert message in
AngularJS or show / display JavaScript alert box or message in AngularJS using ng-click with example. Generally in JavaScript we will use “window” property to show alert message same way in AngularJS we will use “$window” property show alert message.


To show alert message in AngularJS we need to write the code like as shown below


$scope.showalertmessage = function () {
if ($scope.username == undefined)
$window.alert('Please Enter Username')
else
$window.alert('Entered Name is : ' + $scope.username)
}

If you want to check it in complete example you need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Angularjs Show JavaScript Alert Message</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var myapp = angular.module('sampleapp', []);
myapp.controller("expressionController", function ($scope,$window) {
$scope.showalertmessage = function () {
if ($scope.username == undefined)
$window.alert('Please Enter Username')
else
$window.alert('Entered Name is : ' + $scope.username)
}
})
</script>
</head>
<body>
<form id="form1">
<div data-ng-app="sampleapp" data-ng-controller="expressionController">
Enter Name : <input type="text" ng-model="username" />
<input type="button" value="Show Alert" ng-click="showalertmessage()" />
</div>
</form>
</body>
</html>

Live Demo

For live demo enter text in below textbox and click below button to see alert message


Enter Name :

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Unknown said...

Good code, but i want by default get value alert show data

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.