2016-03-11 169 views
0

我試着翻譯基於角1.4.8過濾器不帶選項

我創建了一個簡單的測試,如果任何項目可以將字符串與自定義過濾器選項項目「123」

app.filter('mytranslation', ['$rootScope', function($rootScope) { 
    return function(t_str) { 
    return t_str + "123"; 
    }; 
}]); 
工作

不過,我得到這個anbiguous例外,

Error: [$injector:unpr] Filter

而關於NG過濾其他教程不工作爲好。

都HAML代碼在控制檯上無法工作太

%select{"ng-options" => "item.id as item.from for item in routes | mytranslation ", "ng-model"=>"selected"} 
%select{"ng-options" => "item.id as (item.from | mytranslation) for item in routes ", "ng-model"=>"selected"} 

異常

Error: [$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=mytranslationFilterProvider%20%3C-%20mytranslationFilter 
     at Error (native) 
     at http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:7:416 
     at http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:42:121 
     at Object.d [as get] (http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:40:92) 
     at http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:42:195 
     at Object.d [as get] (http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:40:92) 
     at http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:150:129 
     at W (http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:112:209) 
     at http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:110:334 
     at n (http://localhost:3000/assets/angular/angular.min-b5b8b02773bd468b763522da33293b86.js?body=1:8:333) <select ng-model="selected" ng-options="item.id as item.from for item in routes | mytranslation " class="ng-pristine ng-untouched ng-valid"> 

更新版2

路由(對象陣列)

[ 
{ 
from: "BANGKOK", 
to: [ 
"KAOHSIUNG", 
"TAIPEI", 
"OSAKA", 
"TOKYO", 
"SINGAPORE" 
] 
}, 
{ 
from: "CHIANGMAI", 
to: [ 
"TAIPEI" 
] 
}, 
] 

的fileter未觸發

app.filter('mytranslation', function(data){ 
    return data + "123"; 
}); 

生成的HTML(不作爲我的期望)

<select ng-model="selected" ng-options="item.id as item.from for item in routes | filter:mytranslation" class="ng-pristine ng-untouched ng-valid"> 
     <option label="BANGKOK" value="undefined:undefined">BANGKOK</option> 
     ..... 
     <option label="CHIANGMAI" value="undefined:undefined">CHIANGMAI</option> 

期望的結果應該是

<select ng-model="selected" ng-options="item.id as item.from for item in routes | filter:mytranslation" class="ng-pristine ng-untouched ng-valid"> 
     <option label="BANGKOK" value="BANGKOK">BANGKOK123</option> 
     ..... 
     <option label="CHIANGMAI" value="CHIANGMAI">CHIANGMAI123</option>  
+1

我很確定這行'item.id as item.from for item in routes | mytranslation'應該填入'item.id as item.from for route |中的項目過濾器:mytranslation' –

+0

首先爲什麼注入$ rootScope不使用它? –

+0

@AlonEitan請參閱更新版本2,它仍然不起作用,謝謝 – newBike

回答

0

UPDATE

你不需要在HTML中指定filter:。只需在那裏指定過濾器名稱(mytranslation)。像這樣

<select ng-model="selected" ng-options="item.id as (item.from | mytranslation) for item in routes" class="ng-pristine ng-untouched ng-valid"></select> 

而且你的過濾器應該是這樣的JS

app.filter('mytranslation', function(){ 
    return function(data){ 
     return data + "123"; 
    } 
}); 

在這裏創造一個工作的jsfiddle http://jsfiddle.net/WfuAh/151/

+0

爲什麼這是錯誤的? ['$ rootScope',函數($ rootScope){ 我認爲這是爲了縮小? –

+0

@DivakarDass請參閱更新版本2,它仍然不起作用,謝謝 – newBike

+0

@newBike剛剛更新了我的答案與工作JSFiddle。 –

0

無效語法版本

app.filter('mytranslation', function(data){ 
    return data + "123"; 
}); 

有效版本

app.filter('mytranslation', function(){ 
    return function(data){ 
    return data + "123"; 
    } 
}); 
+0

我想說,你應該建議編輯一個答案,而不是添加自己的.. –