0xAX
0xAX

Reputation: 21817

AngularJS apply filter in controller

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.

Upvotes: 0

Views: 678

Answers (1)

joakimbl
joakimbl

Reputation: 18081

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');
}

Upvotes: 1

Related Questions