I have 4+ experience and working as software Engineer with Hytech professionals India PVT ltd and also working as Consultant,Trainer,Blogger and Author on Microsoft technologies,Also I have Trained more than 500+ IT professionals.I have knowledge about Asp.net,C#,SQL,Project management,replication,MVC,WebApi,SSRS,PostMan,TFS,SVN,Swagger,WMI,Agile,SignalR,Angularjs,IIS configuration.

Part 9::What is $apply() and $filter in AngularJs?

$apply():-

The $scope.$apply() work takes a capacity as parameter which is executed, and after that $scope.$digest() is called inside. That makes it less demanding for you to ensure that all watches are checked, and in this way all information ties invigorated. Here is a $apply()
Example:-
$scope.$apply(function() {
$scope.data.myVar = "Another worth";
});

$filter():-

There are two way use filter in angular js
1:-filter in view or HTML page with expression {{--|--}}.
AngularJS provides filters to transform data by default using in expression with HTML tag:
  • currency ::Format a number to a currency format.
  • date ::Format a date to a specified format.
  • filter ::Select a subset of items from an array.
  • json ::Format an object to a JSON string.
  • limitTo ::Limits an array/string, into a specified number of elements/characters.
  • lowercase ::Format a string to lower case.
  • number ::Format a number to a string.
  • orderBy ::Orders an array by an expression.
  • uppercase ::Format a string to upper case.
<p>The name is {{ lastName | uppercase }}</p>  
You can use filter properties after {{--|--}} with any HTML tag.
2:-filter in Angular controller.
Step 1:-Create Html page using Angular.js file
<div ng-app>
<div ng-controller="Demo">
<input type="text" ng-model="search">
<table>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
</tr>
</tbody>
</table>
</div>
</div>
Step 2:-Create .Js file 
Step 3:-Inject $filter in your controller.
function Demo($scope, $filter)
{
$scope.items = [
{id:1, name:'Bikesh'},
{id:2, name:'srivastava'},
{id:3, name:'Alok'},
{id:4, name:'srivastava'},
{id:5, name:'Hitesh singh'}];
$scope.items2 = $scope.items;
$scope.$watch('search', function(val)
{
$scope.items = $filter('filter')($scope.items2, val);
});
};
You have just read an article that categorized by title AngularJs by title Part 9::What is $apply() and $filter in AngularJs?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/01/part-9what-is-apply-and-filter-in.html. Thank You!
Author: Bikesh Srivastava - Sunday, 31 January 2016

There are currently no comments for "Part 9::What is $apply() and $filter in AngularJs?"

Post a Comment

Life Is Complicated, But Now programmer Can Keep It Simple.