2012-12-17 50 views

回答

2

最好的辦法是通過將數據作爲一個附加參數:<span>{{ value | filter:anotherValue }}</span>。這樣,valueanotherValue是控制器的範圍變量。 filter是您的自定義過濾器。過濾器將收到anotherValue內容作爲第二個參數,並且對其進行更改將重新應用過濾器。這是一個fiddle

另一種解決方案是,如果您不適合隨時傳遞信息來編寫過濾器,則是在它們之間使用共享服務。然後,您可以從控制器中公開屬性並在過濾器中使用。另一個fiddle here(只需更改輸入文本值)。

基本上,這是用小提琴會發生什麼:使用服務來共享數據

// sets the shared data 
mod.controller('Ctlr', function(sharedService) { 
    sharedService.data = 'something'; 
} 

// uses the shared data 
mod.filter('customFilter', function(sharedService) { 
    return function(input) { 
    return input + sharedService.data; 
    }; 
}); 

// the shared service that holds the data 
mod.service('sharedService', function(){ 
    this.data = 'default value'; 
}); 
+0

是一個好主意。非常感謝。它真的幫了我:-) –

+0

我想看我的$範圍變量,這使得異步調用文件。怎麼做??? –

+0

沒有明白。你究竟想看什麼? –

3
在控制器

$scope.variable=['1','2','3']; 

鑑於

<span>{{ variable | f1 }} 

在濾波器

angular.module('Filters', ['Services']).filter('f1', function() { 
var events=Events.query(); 
return function(input) { 
alert(input)->['1','2','3'] 
}; }); 
相關問題