0

I have root controller in my AngularJS web application and filter. Filter works if i apply it in the html template, but it doesn't work if i'm trying to apply filter in the controller.

function Controller ( ... deps ...) {
    filter = $filter('my_filter');
    $scope.$apply(function(){$scope.error_message =  filter('ERROR');});
}

filter must returns simple error string to the <p>, it doesn't work.

If i make:

<p>{{'....' | my_filter}}</p>

It works. Why?

Thank you.

1 Answer 1

1

Don't wrap $scope.error_message = filter('ERROR'); in $scope.$apply - this will cause an error because the Controller is invoked in a digest cycle.

This should work:

function Controller ($filter ... other deps ...) {
  var filter = $filter('my_filter');
  $scope.error_message =  filter('ERROR');
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if i want to do the exact same thing on a directive? stackoverflow.com/questions/17202242/…

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.