Angular Filter Service Inside a Controller

The first message is always so soft!

There was a problem inside Angular 1.3 and using the Stateful filter in the controller.

In short, when you use the $ filter ('custom') (data) method as a pose for {{data | custom}} - and cutom filter updates (say, service updates), only inline updates ({{}}) in the view.

Plunkr: http://plnkr.co/edit/nukioL3ORnl9KDPHmNoY?p=preview

(function(angular) {
  'use strict';
  angular.module('myStatefulFilterApp', [])
    .filter('decorate', ['decoration',
      function(decoration) {

        function decorateFilter(input) {
          return decoration.symbol + input + decoration.symbol;
        }
        decorateFilter.$stateful = true;

        return decorateFilter;
      }
    ])
    .controller('MyController', function($filter, $scope, decoration) {
      $scope.greeting = 'hello';
      // This will update
      $scope.decoration = decoration;
      // This won't
      $scope.code = $filter('decorate')($scope.greeting);
    })
    .value('decoration', {
      symbol: '*'
    });
})(window.angular);

I tried a number of solutions to this problem, including event listeners, etc., but this example makes it clear that none of them will work.

Initially, it proceeded from the use of the date filter and the locale plugin, which dynamically updates the locale, so that when language loads are loaded, the filter was not updated.

, , , ( $watch).

.

+4
3

, , . $filter('decorate')($scope.greeting) , $scope.code, {{greeting | decorate}} . , {{greeting | decorate}} greeting. .

$scope.code $scope.greeting , $scope.$watch:

$scope.$watch('greeting', function(newValue) {
    $scope.code = $filter('decorate')(newValue);
});
+3
    I think you should try to use $watch.
    It will resolve the problem. 

    Here is the updated script.

http://plnkr.co/edit/4IlBIhh2JfmPcMDW0ty6?p=preview

    Hope this will help you.

    Thanks.
+1

, $watch, , ng- , , .

  $scope.update = function(){
    $scope.code =$filter('decorate')($scope.greeting);
    };

http://plnkr.co/edit/uPqCz2EpvjNrwCM0ztLs?p=preview

0
source

All Articles