在这个小提琴中:http://jsfiddle.net/CBgcP/742/
我试图通过onclick将自定义过滤器的结果传递给作用域函数:
<span ng-click="open(friend.name | custom)")
此语法不正确。可以通过单击鼠标将过滤操作的结果传递给函数吗?
小提琴src:
<div ng:app="myApp">
<div ng-controller="HelloCntl">
<ul>
<li ng-repeat="friend in friends | weDontLike:'Adam'">
<span ng-click="open(friend.name | custom)")>{{friend.name}}</span>
<span>{{friend.phone}}</span>
</li>
</ul>
</div>
</div>
span
{
width: 200px;
}
angular.module('myApp',[]).filter('weDontLike', function(){
return function(items, name){
var arrayToReturn = [];
for (var i=0; i<items.length; i++){
if (items[i].name != name) {
arrayToReturn.push(items[i]);
}
}
return arrayToReturn;
};
});
angular.module('myApp', []).filter('custom', function() {
return function(input) {
return input.substring(0 , 2)
};
});
function HelloCntl($scope) {
$scope.open = function(){
console.log('in open');
}
$scope.friends = [
{
name: 'John',
phone: '555-1276'},
{
name: 'Mary',
phone: '800-BIG-MARY'},
{
name: 'Mike',
phone: '555-4321'},
{
name: 'Adam',
phone: '555-5678'},
{
name: 'Julie',
phone: '555-8765'}
];
}
答案 0 :(得分:1)
您可以使用$filter
服务执行此操作:
function HelloCntl($scope, $filter) {
$scope.open = function(friend){
console.log('in open with friend\'s name: ' + friend);
console.log('friend\'s name with filter applied: ' + $filter('custom')(friend);
}
// ...
}