Ответ 1
Видимо, вы можете.
Обычно вы передавали переменные сферы в фильтр как параметр функции:
function MyCtrl($scope){
$scope.currentDate = new Date();
$scope.dateFormat = 'short';
}
<span ng-controller="MyCtrl">{{currentDate | date:dateFormat}}</span> // --> 7/11/13 4:57 PM
Но, чтобы передать текущую область действия, вам нужно пройти this
:
<span ng-controller="MyCtrl">{{currentDate | date:this}}</span>
и this
будет ссылкой на текущую область действия:
Упрощенная:
app.controller('AppController',
function($scope) {
$scope.var1 = 'This is some text.';
$scope.var2 = 'And this is appended with custom filter.';
}
);
app.filter('filterReceiptsForDate', function () {
return function (input, scope) {
return input + ' <strong>' + scope.var2 + '</strong>';
};
});
<div ng-bind-html-unsafe="var1 | filterReceiptsForDate:this"></div>
<!-- Results in: "This is some text. <strong>And this is appended with custom filter.</strong>" -->
Внимание:
- Будьте осторожны с этим и используйте область только для чтения значений внутри фильтра, потому что в противном случае вы легко найдете свое "я" в цикле $digest.
- Фильтры, требующие такой "тяжелой" зависимости (весь объем), как правило, очень трудно тестировать.