我试图为元素数组创建一个角度为2的管道,以便它基于所选的false过滤仅那个元素,
我的数组
this.states = [ {name: 'John', selected: false, value: 1}, {name: 'Bill', selected: false, value: 2},
{name: 'Smith', selected: false, value: 3}, {name: 'Alex', selected: false, value: 4},
{name: 'Martin', selected: false, value: 5}, {name: 'James', selectes: false, value: 6}];我需要过滤选择为false的值,
我的管道代码
import {Injectable,Pipe} from 'angular2/core';
@Pipe ({
name : 'restrictValues'
})
@Injectable()
export class restrictValues implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => item.id.indexOf(args[1]) !== true);
}
}我的HTML实现
<select ngControl="select_state" (change)="statechange()" #select_state="ngForm" class="form-control btn btn-primary">
<option *ngFor="#statez of states | restrictValues : false" value="{{statez.value}}">
{{statez.name}}
</option>
</select>管道未按预期工作,如果代码中有任何错误,请纠正我
发布于 2016-07-04 14:05:25
@Pipe({
name:'restrictValues'
})
@Injectable()
export class restrictValues implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => item.selected === false);
}
}请注意,您的代码示例实际上并没有查看对象的selected属性,而是选择了id。
发布于 2016-07-04 14:07:25
您可能需要将管道弄脏。
@Pipe ({
name : 'restrictValues',
pure: false
})否则,当在states中添加/删除/修改项时,它将不会被调用。
这会导致管道在每个更改检测周期执行。您可以考虑使用主动通知更改的可观察对象。
此外,在较新的Angular2版本中(从~rm1开始),可选参数不再作为数组传递
transform(items: any[], args: any[]): any {应该是
transform(items: any[], param1?:any, param2?:any): any {取决于您希望支持的参数数量
发布于 2016-07-04 14:11:49
import {Injectable,Pipe} from 'angular2/core';
@Pipe ({
name : 'restrictValues'
})
@Injectable()
export class restrictValues implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => !item.selected);
}
}https://stackoverflow.com/questions/38177791
复制相似问题