2015-09-09 104 views
1

我正在使用angularjs 1.4.4

我試圖更新$ locale動態並且它正在更新。當過濾器運行時,它使用正確的更新$ locale來評估自己。不幸的是舊的過濾器不會重新運行。

var englishMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 
var frenchMonths = ["janvier", "f\u00e9vrier", "mars", "avril", "mai", "juin", "juillet", "ao\u00fbt", "septembre", "octobre", "novembre", "d\u00e9cembre"]; 

$scope.array = []; 
$scope.currentDate = 'english'; 

$scope.addDate = function(){ 
    $scope.array.push(new Date()); 
}; 
$scope.switchDate = function(){ 
    var monthArray = englishMonths 
    if($scope.currentDate === 'english'){ 
     monthArray = frenchMonths; 
     $scope.currentDate = 'french'; 
    } 
    else{ 
    $scope.currentDate = 'english'; 
    } 
    $locale.DATETIME_FORMATS.MONTH.length = 0; 
    for(var x = 0; x < monthArray.length; x++){ 
     $locale.DATETIME_FORMATS.MONTH.push(monthArray[x] + ($scope.currentDate === 'french' ? "FRENCH" : '')); 
    } 
}; 

而且在DOM:

<button data-ng-click="addDate()">Add Date</button>  
<button data-ng-click="switchDate()">Toggle Language, current: {{ currentDate }}</button> 
<hr></hr> 
<div data-ng-repeat="date in array"> 
    {{ date | date:'longDate' }} 
</div> 

如果您添加日期到陣列將在當前的語言評價。如果您更改語言,舊的日期將不會重新評估。

您可以在我所做的小提琴看到這一點:http://jsfiddle.net/wf8ojqjg/

所以我需要一種方法爲$語言環境進行更新,這實際上可以歸結爲迫使所有的過濾器重新計算。

+0

怎麼樣的解決方案,像[這](http://jsfiddle.net/davidepastore/wf8ojqjg/1/)? –

+0

我有數組遍佈我的應用程序,所以這是行不通的。 –

+0

它們是否共享相同的結構?您可能有興趣使用[服務](https://docs.angularjs.org/guide/services)。 –

回答

0

因此,基於一些研究它看起來像要做到這一點的唯一方法是覆蓋原來的datefilter像這樣:

.config(function ($provide) { 
    $provide.decorator('dateFilter', function ($delegate) { 
     $delegate.$stateful = true; 
     return $delegate; 
    }); 
}) 

這是無證只有在角新版本存在。你可以看到這個修補程序在小提琴上工作:http://jsfiddle.net/y8bobe6g/

0

所以看起來這可能不能直接與$ locale服務。看看這個鏈接SO有人基本上想要做同樣的事情:

Angularjs and $locale

0

有趣的問題!

正如其他人所說,過濾器不會被摘要循環更新,因爲它們「觀察」的對象的實例不會更改,因此它們不會被重新應用。

此「問題」的一個解決方法是在您的示例中使用「新」日期重新生成日期數組,因此過濾器將被觸發。

看看這個更新的jsfiddle:http://jsfiddle.net/wf8ojqjg/3/

angular 
    .module('app', []) 
    .controller('ParentCtrl', ParentCtrl) 

function ParentCtrl($scope, $locale) { 

    var englishMonths = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; 
    var frenchMonths = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 
     'octobre', 'novembre', 'décembre']; 

    $scope.arrays = { 
     dates: [] 
    }; 
    $scope.currentDate = 'english'; 

    $scope.addDate = function(){ 
     $scope.arrays.dates.push(new Date()); 
    }; 

    $scope.switchDate = function(){ 
     var monthArray = englishMonths 
     if($scope.currentDate === 'english') { 
      monthArray = frenchMonths; 
      $scope.currentDate = 'french'; 
     } else { 
      $scope.currentDate = 'english'; 
     } 
     $locale.DATETIME_FORMATS.MONTH = monthArray; 
     $scope.arrays.dates = renewInstancesOfArrayOfDates($scope.arrays.dates); 
    }; 

    var renewInstancesOfArrayOfDates = function(array) { 
     var temp = []; 

     angular.forEach(array, function(t) { 
      temp.push(new Date(t)); 
     }); 

     return temp; 
    }; 
} 

我沒有這可以被認爲是正確的,也沒有完美的解決方案,因爲有在再生陣列和消耗越來越多的內存沒有任何意義。

使用它的責任。

+0

我已經遍佈我的應用程序,不能再生數組。 –