我试着翻译基于角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