0

I try to use filter angularjs service in my chrome extension. But i have no idea how to get/inject it in my function. For the moment i have:

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    result = $filter('filter')(jsonArray, info.selectionText, function (actual, expected) {
        return actual.toString().toLowerCase().indexOf(expected.toLowerCase()) == 0;
    });
    console.log(sug);
});

But obviously i have: $filter is not defined. I also want to use $http service to get my jsonArray

4
  • Inject dependencies in the controller/provider/module in which the function is Commented Sep 3, 2015 at 11:35
  • i have no controller/provider/module. I just want to use filter alone. Commented Sep 3, 2015 at 11:36
  • You don't need all of angular just to filter an array. It's built in since ES5, see developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 3, 2015 at 11:37
  • Ok! But i also use $http to get my json array.. So in general how to use angularjs service without controller/provider/module? Commented Sep 3, 2015 at 11:46

2 Answers 2

1

where-ever you are using $filter, you need to add the dependency for the same. you can add the dependency by providing a $filter as a parameter to main function.

if adding inside controller :

    .controller('MainCtrl', function($scope, $filter) {
      $scope.originalText = 'hello';
      $scope.filteredText = $filter('uppercase')($scope.originalText);
});

if adding inside factory/service :

app.factory('myService', ['$filter', function($filter)

this link may help you for more information : Inject dateFilter in a service in AngularJs

Sign up to request clarification or add additional context in comments.

2 Comments

Ok but how to use myService without controller ?
1

The solution is:

var myInjector = angular.injector(["ng"]);
var $filter = myInjector.get("$filter");

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.