定義,我希望用戶擁有更改格式的選項格式化的飛行日期,所以我創建了一個過濾器這樣
<ul>
<li ng-repeat"item in items">
<h2>{{item.date | formatDate }} </h2>
</li>
</ul>
然後在我的代碼
myapp.filter('formatDate', function(){
return function(date) {
return formatDateFunction(date)
}
}
function formatDateFunction(unix_epoch){
var date = new Date(unix_epoch);
var language = $scope.desiredLanguage
var time = date.toLocaleTimeString(language, options);
return time;
}
myapp.controller('MyCtrl',[$scope,
function MyCtrl($scope){
$scope.desiredLanguage = 'en-us';
--code omitted
的問題是,過濾器調用需要在控制器的$範圍內定義的值,使得我現在寫的代碼的方式,值是一個函數不可用,即$ scope在format
filt中不可用呃,也不是它所調用的formatDateFunction
。
問題:如何訪問上面的過濾器中的示波器,或者相反,如何安排它以使<h2>{{item.date | formatDate }} </h2>
在控制器上調用過濾器?
嘗試在formatDateFunction函數中使用this.desiredLanguage –
@JorgeCasariego不起作用。 formateDateFunction中的'this'是'undefined''formatDateFunction'和定義了'$ scope.desiredLanguage'的'MyCtrl'之間沒有連接,即$ scope在formatDateFunction中不存在。 – BrainLikeADullPencil