angularjs filter, element and angular.element
Blogs20152015-01-14
1. Angularjs filter
According to the document, the usage of `filter` like this:
- In HTML Template Binding: {{ filter_expression | filter : expression : comparator}}
- In JavaScript: $filter(âfilterâ)(array, expression, comparator)
I have an `search` input field, and a `summary` div, which interact to show results.The following is my `$watch` function to monitor `search` input, and filter to display the number of records.
//1. real-time monitor:
$scope.$watch('searchText', function (newVal, oldVal) {
if (newVal === oldVal) return;
var len=$filter('filter')($scope.response, {name: $scope.searchText}).length;
$scope.totalRecords = len || 0;
});
//2 the HTML:
<input type="search" placeholder="{{placeholder}}" ng-model="searchText">
...
<div class="summary" ng-model="totalRecords">{{totalRecords}}</div> records found.
2. element, angular.element
Whatâs the difference of `element` and `angular.element` in directiveâs link?
- element (params of directiveâs link): element is the jqLite-wrapped element that this directive matches.
- angular.element (HTML string | DOMElement): If jQuery is available, angular.element is an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angularâs built-in subset of jQuery, called âjQuery liteâ or âjqLite.â
so they are the same. When I run element===angular.element(element), it returns true, that means they are equal. The following is a good way to use both of them:
var dom = element[0].querySelector('[header]');
angular.element(dom).css({display: 'none'});
3. Which expression is better?
var total = len ? len : 0;
var total = len || 0;
var calls=this._callbacks || (this._callbacks={});
(this._callbacks[ev] || (this._callbacks[ev]=[])).push(callback);