我試着翻譯基於角1.4.8過濾器不帶選項
我創建了一個簡單的測試,如果任何項目可以將字符串與自定義過濾器選項項目「123」
app.filter('mytranslation', ['$rootScope', function($rootScope) {
return function(t_str) {
return t_str + "123";
};
}]);
工作
不過,我得到這個anbiguous例外,
Error: [$injector:unpr] Filter
而關於NG過濾其他教程不工作爲好。
- AngularJS : ng-repeat filter when value is greater than
- How can I add internationalization to a select drop down with AngularJS?
都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>
我很確定這行'item.id as item.from for item in routes | mytranslation'應該填入'item.id as item.from for route |中的項目過濾器:mytranslation' –
首先爲什麼注入$ rootScope不使用它? –
@AlonEitan請參閱更新版本2,它仍然不起作用,謝謝 – newBike