Module 5
Module 5
Ex.
</div>
</div>
AngularJS Services
In AngularJS you can make your own service, or use one of the many
built-in services.
What is a Service?
In AngularJS, a service is a function, or object, that is available for, and
limited to, your AngularJS application.
The $location service has methods which return information about the
location of the current web page:
Example
Use the $location service in a controller:
Example
Display a new message after two seconds:
Example
Display the time every second:
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
To use your custom made service, add it as a dependency when defining the
controller:
AngularJS Events
You can add AngularJS event listeners to your HTML elements by using one
or more of these directives:
• ng-blur
• ng-change
• ng-click
• ng-copy
• ng-cut
• ng-dblclick
• ng-focus
• ng-keydown
• ng-keypress
• ng-keyup
• ng-mousedown
• ng-mouseenter
• ng-mouseleave
• ng-mousemove
• ng-mouseover
• ng-mouseup
• ng-paste
An AngularJS event will not overwrite an HTML event, both events will be
executed.
Mouse Events
Mouse events occur when the cursor moves over an element, in this order:
1. ng-mouseover
2. ng-mouseenter
3. ng-mousemove
4. ng-mouseleave
Example
Increase the count variable when the mouse moves over the H1 element:
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
</script>
Form Validation
AngularJS offers client-side form validation.
AngularJS monitors the state of the form and input fields (input, textarea,
select), and lets you notify the user about the current state.
AngularJS also holds information about whether they have been touched, or
modified, or not.
You can use standard HTML5 attributes to validate input, or you can make
your own validation functions.
Client-side validation cannot alone secure user input. Server side validation is also
necessary.
Required
Use the HTML5 attribute required to specify that the input field must be
filled out:
Example
The input field is required:
<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>
E-mail
Use the HTML5 type email to specify that the value must be an e-mail:
Example
The input field has to be an e-mail:
<form name="myForm">
<input name="myInput" ng-model="myInput" type="email">
</form>