Arrays 需要为angular2中的元素数组创建管道
我正在尝试为元素数组创建一个角度为2的管道,以便它根据所选的false过滤该元素 我的阵列Arrays 需要为angular2中的元素数组创建管道,arrays,angularjs,angular,angular2-pipe,Arrays,Angularjs,Angular,Angular2 Pipe,我正在尝试为元素数组创建一个角度为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:
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>
{{statez.name}
如果代码中有任何错误,请纠正我
@Pipe({
name:'restrictValues'
})
@Injectable()
export class restrictValues implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => item.selected === false);
}
}
请注意,您的代码示例实际上并不是在查看对象的selected
属性,而是在选择id
请注意,您的代码示例实际上并没有查看对象的
selected
属性-它正在选择id
您可能需要使管道不纯净
@Pipe ({
name : 'restrictValues',
pure: false
})
否则,当在状态下添加/删除/修改项时,将不会调用它
这将导致在每个更改检测周期执行管道。您可以考虑使用可观察到的主动通知更改。
在较新的Angular2版本中(自~RC.1以来),可选参数不再作为数组传递
transform(items: any[], args: any[]): any {
应该是
transform(items: any[], param1?:any, param2?:any): any {
根据要支持的参数数量,可能需要使管道不纯净
@Pipe ({
name : 'restrictValues',
pure: 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.selected);
}
}
否则,当在状态下添加/删除/修改项时,将不会调用它
这将导致在每个更改检测周期执行管道。您可以考虑使用可观察到的主动通知更改。
在较新的Angular2版本中(自~RC.1以来),可选参数不再作为数组传递
transform(items: any[], args: any[]): any {
应该是
transform(items: any[], param1?:any, param2?:any): any {
根据您要支持的参数数量如果已在另一个选择框中使用angular 2选择了选定选项,我将尝试从选定框中删除选定选项您对此有何想法?如果已在另一个选择框中使用angular 2选择了选定选项,我将尝试从选定框中删除选定选项您有何想法在那上面
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);
}
}