我只需要过滤categoria
为a
的那些对象。有什么办法可以使用ngFor做到这一点?
service.ts:
productos: Producto[] = [
{id:'01', categoria:'a', titulo:'title1'},
{id:'02', categoria:'b', titulo:'title2'},
{id:'03', categoria:'a', titulo:'title3'},
{id:'04', categoria:'c', titulo:'title4'},
{id:'05', categoria:'c', titulo:'title5'}
]
getProductos() {
return of(this.productos);
}
component.ts:
ngOnInit() {
this.productos$ = this.ps.getProductos();
}
component.html:
<div *ngFor="let producto of productos$ | async">
{{ producto.titulo}}
</div>
答案 0 :(得分:1)
您可以使用管道(请参阅Vinit链接)或使用过滤器
import random
import sys
K_SIZE = 1000
class Generator:
def __init__(self,low=4,high=10):
self.table = {i:chr(i) for i in range(33,127)}
self.low = low
self.high = high
def create_n_bytes(self,total_bytes):
bytes_created = 0
"""Hack at the moment, this condition will fail only after more than n bytes are
written """
while bytes_created < total_bytes:
bytes_to_create = random.randint(self.low,self.high)
bytes_created = bytes_created+bytes_to_create+1
word=[""]*bytes_to_create
for i in range(bytes_to_create):
word[i] = self.table[random.randint(33,126)]
text = "".join(word)
#print(str(hash(text))+"\t"+text)
print(text)
def get_file_size_from_command_line():
size = sys.argv[1]
return size
def get_min_word_len_from_command_line():
if len(sys.argv) > 2:
low = sys.argv[2]
return int(low)
def get_max_word_len_from_command_line():
if len(sys.argv) > 3:
high = sys.argv[3]
return int(high)
def get_file_size_in_bytes(size):
multiplier = 1
size_unit = size[-1]
if size_unit == 'M' or size_unit == 'm':
multiplier = K_SIZE*K_SIZE
elif size_unit == 'K' or size_unit == 'k':
multiplier = K_SIZE
elif size_unit == 'G' or size_unit == 'g':
multiplier = K_SIZE*K_SIZE*K_SIZE
elif size_unit in ('0','1','2','3','4','5','6','7','8','9'):
multiplier = 1
else:
print("invalid size")
exit()
total_bytes = 0
if multiplier == 1:
total_bytes = int(size)
else:
total_bytes = multiplier*int(size[:len(size)-1])
return total_bytes
def main():
if len(sys.argv) == 2:
gen = Generator()
elif len(sys.argv) == 3:
gen = Generator(get_min_word_len_from_command_line())
elif len(sys.argv) == 4:
gen = Generator(get_min_word_len_from_command_line(),get_max_word_len_from_command_line())
file_size = get_file_size_in_bytes(get_file_size_from_command_line())
gen.create_n_bytes(file_size)
if __name__== "__main__":
main()
答案 1 :(得分:0)
您可以使用过滤器运算符来做到这一点。
ngOnInit() {
this.productos$ = this.ps.getProductos().pipe(filter(el => el.categoria == 'a'));
}