• Blogs (9)
    • đŸ“± 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • 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);