Доступ к фильтру в директиве
Измучился совсем. Нэразумэю.
MyApp = angular.module('MyApp', []); MyApp.filter('MyFilter', function(){ // do something... }); MyApp.directive('MyDirective', function(){ // Как мне вот тут получить доступ к $filter('MyFilter')? }); |
MyApp.directive('MyDirective', ['$filter', function($filter){ $filter('MyFilter')(options); }]); |
Не взлетает.
MyApp.directive('MyDirective', ['$filter', function(){ return { restrict: 'A', require: 'ngModel', link: function($scope, element, attrs, ngModel, $filter){ ngModel.$formatters.push(function(text){ return $filter('MyFilter')(text); }); } } }]); Object is not a function. |
Так сложно скопировать мою первую строчку?
|
Пардон.
MyApp.directive('MyDirective', ['$filter', function($filter){ return { restrict: 'A', require: 'ngModel', link: function($scope, element, attrs, ngModel, $filter){ ngModel.$formatters.push(function(text){ return $filter('MyFilter')(text); }); } } }]); Не взлетает. Object is not a function. |
link: function($scope, element, attrs, ngModel, $filter){ Сюда фильтр зачем добавил? В моем примере не было. |
Взлетело!
Если не затруднит, расскажи, правила применения передачи аргументов. Или сошли на что нибудь толковое. |
https://docs.angularjs.org/api/ng/service/$filter
|
Часовой пояс GMT +3, время: 03:17. |