ロケールに基づいてメインリストをフィルタリングするカスタムフィルタを追加しようとしました。メインリストを取得するには、私はajax呼び出しを入れています。デバッグしようとしているうちに、app.filter
がajaxが応答を提供する前に実行されているようです。以下は、私が取得していますエラーです:モデルが利用可能になる前に呼び出されるapp.filterを使用するカスタムフィルタ
angular.js:15712TypeError: Cannot read property 'length' of undefined at messageSelectionCtrl.js:57 at fn (eval at (angular.js:14059), :4:360) at Object. (angular.js:16730) at r.$digest (angular.js:18747) at r.$apply (angular.js:19054) at g (angular.js:13192) at T (angular.js:14196) at XMLHttpRequest.w.onload (angular.js:14364)
ここで私はそれぞれにしようとしているかについてのコードに見てです:
messageSelectionCtrl.js:
//getMessageSelectionConfigs is the service used to make http request and the successhandler and errorhandler defined in controller.
app.controller('messageSelectionCtrl',['$scope','getMessageSelectionConfigs', 'filterData', '$log','errorService', function($scope, getMessageSelectionConfigs, filterData, $log, errorService){
$scope.filterData = filterData;
....
...
//service call to fetch message selection config
getMessageSelectionConfigs.get(data).then(messageSelectionSuccessHandler,messageSelectionErrorHandler);
......
}]);
.......
.....
app.filter('filterByLocale', function(){
return function (messageSelectionList, locale){
var filtered = [];
for (var i=0; i<messageSelectionList.length; i++){
var item= messageSelectionList[i];
if(item.locale === locale){
filtered.push(item);
}
}
return filtered;
};
});
.....
..
messageSelectionTemplate.html
エラーが来た後、0<div ng-repeat = "item in messageSelectionList | filterByLocale: filterData.locale">
mainCtrl.js
app.factory('filterData', function(){
return {
locale : ''
};
});
messageSelectionSuccessHandler
機能が実行されます。これは、ng-repeatがループするいくつかのモデルを持っていれば、ビューがトリガされるべきであるために変です。 私に何が欠けているか、何か参考になることを教えてください。
umm..Doingを。フィルタパラメータが空の文字列で初期化されているためですか?デバッグすると 'filterByLocale'は空の配列を返します。私が望むのは、リストを最初にレンダリングし、ロケールが変更されたときにビューが再び変更されるようにすることです。 –
if(フィルター)をフィルターコードの周りに置き、空のフィルターを無視して、変更されていないデータ – dmoo
はい、それは魅力的に機能しました。問題を見つけて手助けする努力を感謝します。 –