angularjs

How to re-render angular filters  #angularjs #reactjs

  • I need to re-render the filters once I programmatically load the appropriate language file.
  • Filters will execute on each $digest but this is inadvisable by perfm.
  • In templates, filters are only executed when their inputs have changed.
  • The filters are part of this $watch evaluation, so you just need to trigger a digest cycle.
  • If we want the filter to be stateless, but responding to changes of some external state, we should make this state to be an input to the filter.

Is there a way to re-render AngularJS filters? I am trying to use the angular currency filter that displays the currency symbol based on the language file that’s loaded. I need to re-render the filters once I programmatically load the appropriate language file.

@ng_real_ninja: How to re-render angular filters #angularjs #reactjs

Is there a way to re-render AngularJS filters? I am trying to use the angular currency filter that displays the currency symbol based on the language file that’s loaded. I need to re-render the filters once I programmatically load the appropriate language file.

Yes. For each expression on your template there is a $watch setup. The filters are part of this $watch evaluation, so you just need to trigger a digest cycle. After you load your language file just make sure to call $scope.$apply(), which will re-run all your watches and update your template if the end-result of any watch has changed.

. – Tom Chen Jan 3 ’14 at 15:45

Can you elaborate as to why that would be preferred? $timeout would just defer the $scope.$apply() until the browser rendering thread has gotten control. That doesn’t seem necessary. – dtabuenc Jan 3 ’14 at 16:29

won’t since it’ll create new $digest loop. Please correct me if I’m wrong on this. Thanks – Tom Chen Jan 3 ’14 at 18:12

In the cases where you get an $apply() already in progress error, it means you should not need to use $apply() at all since there is already a digest cycle happening. Timeout is costly, since it triggers an EXTRA digest cycle. It should not be used indiscriminately. – dtabuenc Jan 3 ’14 at 18:32

The Developer Guide/Filters documentation says:

So, if we want the filter to be stateless, but responding to changes of some external state, we should make this state to be an input to the filter. This input can be purely virtual, not having anything to do with the internal logic of the filter, only triggering its execution.

I have recently faced this problem figuring out how to dynamically re-render moment.js output on a locale change event. This is my filter function:

From Angular 1.3 filters are stateless which means filters just will update when theirs input have change.

If you want to update your filter ever, you need to make your filters $stateful.

Filters will execute on each $digest but this is inadvisable by perfm.

angularjs

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.