0

I have a function in controller like this.

cl.translateCall = function () {
$http.get(`${API_URL}/langChange/` + selectedLanguage).
then(function onSuccess(response) {
      input = response.data; 
      console.log(input)
}).
  catch(function onError(response) {
  console.log(response)
});   
 }

I have a custom filter in module like this

   .filter('languageTranslate',['$http', function($http) {  

   return function(input) {
    if(selectedLanguage != null){

    /*want to call controller function*/

      }else{   
            return input;  
      } 
  }


}]);

How do I call the above controller function inside this Angular JS custom filter? I am totally new to Angular JS.

1
  • 2
    Filters don't work well with asynchronous functions. Instead of putting filter functions in controllers, consider putting them in a custom service. Commented Jan 3, 2018 at 11:52

1 Answer 1

1

Well, you can pass the scope to the filter. I dont know how you use the code above, but if you theoretically have :

{{ someVar | languageTranslate }}

Then you can pass the current scope by adding :this

{{ someVar | languageTranslate:this }} 

and call a scope method like this

return function(input, scope) {
  if (scope.selectedLanguage != null) {
    scope.functionName()
  } else {   
    return input 
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.