Angular JS Unit-4
Angular JS Unit-4
Parameter Values:
• arrayexpression: The source array on which the filter will be applied.
• expression: It is used to select the items from the array after the filter conditions
are met. It can be of String type, Function type, or Object type.
• comparator: It is used to determine the value by comparing the expected value
from the filter expression, and the actual value from the object array.
• anyPropertyKey: It is an optional parameter having the special property that is used
to match the value against the given property. It is of String type & its default value
is $.
Example
<html>
<head><script src="angular.min.js"></script></head>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<h3>AngularJS filter Filter</h3>
<ol>
<li ng-repeat="x in names | filter : 'e‘ "> {{ x }} </li>
</ol>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = ['Jani','Carl', ‘esha’,’Rahul’,’Mathew', 'Joey ];
});
</script>
</body></html>
Custom filter
• Sometimes the built-in filters in Angular cannot meet the needs or
requirements for filtering output. In such a case, an AngularJS custom filter
can be created, which can pass the output in the required manner.
• To crate it the same way we have created controllers & modules.
• With the help of angular.filter we can create custom filter.
Syntax:
app.filter( filtername, function()
{ return function (input, value1,value2)
{ var input; return output;}
}
);
Example
<html>
<head> <script src="angular.min.js"> </script>
<script>
var app=angular.module(‘myapp’,[]);
app.controller(‘formctrl’,function($scope)
{ $scope.pname=”sachin”;
$scope.gname=”cricket”;
});
</script></head>
<body >
<div ng-app=”myapp” ng-controller=” formctrl”>
Player’s Name:<input type=”text” ng-model=”pname”/><br>
Game Name:<input type=”text” ng-model=” gname”/><br>
<p> Detail Information: {{pname+” “+gname}}</p>
</div>
</body></html>
<html> <body>
<head> <div ng-app="myapp" ng-controller="formCtrl">
<script src="angular.min.js"></script> <form novalidate>
<script> P_name:<input type="text" ng-model="product.name“/>
var app = angular.module('myapp', []);
app.controller('formCtrl', function ($scope) { P_qut:<input type="number" ng-model="product.qut“/>
$scope.master = {};
$scope.product = {}; Pay_type:<input type="radio" ng-model="product.type"
value="cash“ /> Cash
$scope.save = function (product) {
$scope.master = angular.copy(product); <input type="radio" ng-model="product.type"
}; value="credit“ /> Credit
$scope.reset = function () {
// $scope.product = angular.copy($scope.master); <input type="button" ng-click="reset()" value="Reset"/>
$scope.product = {}; // Clear the product data <input type="button" ng-click="save(product)"
$scope.master = {}; // Clear the master data value="Save"/>
}; </form>
$scope.reset(); <p>Product Form: {{product | json}}</p>
}); <p>Master: {{master | json}}</p>
</script> </div>
</head> </body>
</html>
Angular form validation
• Validation is the process ensuring that data is correct, complete,
accurate.
• Following list of basic angular form validation options available for an
input field:
Directive Description
ng-required It set required attribute on an input field
ng- minlenght It set minlength attribute on an input field
ng- maxlenght It set maxlength attribute on an input field
Ng-pattern It set pattern validation error key if the ng model value does
not match the specified expression.
<html ng-app="validationApp">
<head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script></
head>
<body ng-controller="validationCtrl">
<form name="myForm" novalidate>
Name: <input type="text" id="name" name="name" ng-model="user.name" ng-required="true">
<span ng-show="myForm.name.$error.required">Name is required.</span>
$valid Ng-valid Return true if all the containing forms and controls are valid.
$invalid Ng-invalid Return true if at least one containing control or form is invalid.
$pristine Ng-pristine Return true if user has not interacted with the form yet.
$dirty Ng-dirty Return true if user has already interacted with the form.
$submitted Ng-submit Return true if user has submitted the form even if its invalid
<html ng-app="validationApp">
<head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script> </head>
<body ng-controller="validationCtrl">
<br>
<div ng-show="myForm.$submitted">
<p ng-show="myForm.$valid">Form submitted successfully!</p>
<p ng-show="myForm.$invalid">Form contains errors. Please correct them.</p>
</div>
<script>
angular.module('validationApp', [])
.controller('validationCtrl', function ($scope) {
$scope.user = {};
$scope.submitForm = function () {
if ($scope.myForm.$valid) {
// Form is valid, perform submission logic here
console.log('Form submitted with data:', $scope.user);
}
};
});
</script>
</body>
</html>
Form events
• An Events in AngularJS can be used to perform particular tasks, based on the action taken. Both Angular Event & the
HTML Event will be executed & will not overwrite with an HTML Event.
• ng-mousemove: The movement of the mouse leads to the execution of the event.
• ng-mouseup: Movement of the mouse upwards leads to the execution of the event.
• ng-mousedown: Movement of the mouse downwards leads to the execution of the event.
• ng-mouseenter: Click of the mouse button leads to the execution of the event.
• ng-mouseover: Hovering the mouse leads to the execution of the event.
• ng-cut: Cut operation leads to the execution of the event.
• ng-copy: Copy operation leads to the execution of the event.
• ng-keypress: Press of key leads to the execution of the event.
• ng-keyup: Press of upward arrow key leads to the execution of the event.
• ng-keydown: Press of downward arrow key leads to the execution of the event.
• ng-click: Single click leads to the execution of the event.
• ng-dblclick: Double click leads to the execution of the event.
• ng-focus: It is used to apply custom behavior when an element is focused.
• ng-paste: It is used to specify custom behavior functions when the text in input fields is pasted into an HTML element.
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="myCtrl">
<p>Number: {{ number }}</p>
<button ng-click="incrementNumber()">Increment</button>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.number = 0;
$scope.incrementNumber = function () {
$scope.number++;
};
});
</script>
</body></html>
Custom model Update triggers
• If any changes had made to the content, It will trigger a model update and
form validation.
• With the help of ng-model-options directive, we can override this behavior
and bind only specified events.
• Syntax
<input ng-model=“player.name” ng-model-options=“Object”/>
• E.g.
ng-model-options=“{updateOn:’blur’}” will update and validate only after the
control loses focus.
Ng-model-options
• Debounce: The debounce option introduces a delay before updating the model after the
input control's value changes. It is specified in milliseconds. This can be useful to reduce
the frequency of model updates when the user is typing quickly.
• Allow Invalid: When set to true, this option allows the model to be updated even if the
input control contains an invalid value according to its validation rules. By default, it's set
to false.
• Getter Setter: You can specify custom getter and setter functions to control how the
value is retrieved from and stored in the model. This can be useful for formatting or
transforming data.
<html ng-app="myApp">
<head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script> </head>
<body ng-controller="myCtrl">
<p>Type in the input field below:</p>
<input type="text" ng-model="inputValue" ng-model-options="{ debounce: 500 }">
<p>Model Value: {{ inputValue }}</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.inputValue = '';
$scope.$watch('inputValue', function (newVal, oldVal) {
if (newVal !== oldVal) { console.log('Model Value Updated:', newVal); }
});
});
</script>
</body>
</html>
AngularJS service
• AngularJS service are JavaScript function or object, who’s responsible
to perform specific tasks and can be reused throughout the
application.
• It provide many inbuilt services. In AngularJS services starts with “$”.
• Example: $http, $window, $location etc.
• These AngularJS services interact with controller, model or custom
directives.
$http service