2016-03-11 114 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

我想说,你应该建议编辑一个答案,而不是添加自己的.. –