どうやらできるようです。
通常、スコープ変数を関数パラメーターとしてフィルターに渡します。
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>" -->
PLUNKER
警告:
- これには注意して、フィルター内の値を読み取るためにのみスコープを使用してください。そうしないと、$digest ループで自分自身を簡単に見つけることができます。
- このような「重い」依存関係 (スコープ全体) を必要とするフィルターは、テストが非常に困難になる傾向があります。