2017-10-20 76 views
-1

我试图实现搜索栏使用管道过滤器为我的数组是可点击的(类似配方列表),它过滤我的食谱的名称。它的工作正常,直到我点击一个项目,它仍然有它的旧索引,因此给我错误的recipeDetails组件(另一个配方)。有人可以帮助我解决这个问题。无法显示正确的阵列细节时,它是可点击使用管道过滤器

管道过滤器代码:

import { Pipe, PipeTransform } from '@angular/core'; 
import {Recipe} from "../recipes/recipe.model"; 

@Pipe({ 
    name: 'filter' 
}) 
export class FilterPipe implements PipeTransform { 

    transform(recipes:Recipe[],filteredString:string): any { 
    if(filteredString.length===0){ 
     return recipes 
    }else{ 
     return recipes.filter(recipe=>recipe.name.toLowerCase().includes(filteredString.toLowerCase())); 
    } 
    } 

} 

食谱详细信息编码:

<div class="row"> 
    <div class="col-xs-12"> 
    <div class="row"> 
     <div class="col-xs-12"> 
     <input id="search" type="text" name="search" placeholder="Search for a Recipe" [(ngModel)]="search"> 
     </div> 
    </div> 
    <app-recipes-list-item *ngFor="let recipe of (recipes |filter:search);let i=index" [recipe]="recipe" [index]="i"></app-recipes-list-item> 
    </div> 
</div> 

配方服务代码

import {Recipe} from "../recipes/recipe.model"; 
import {EventEmitter, Injectable} from "@angular/core"; 
import {Ingredient} from "../shared/ingredient.model"; 
import {ShoppingListService} from "./shopping-list.service"; 
import {Subject} from "rxjs/Subject"; 

@Injectable() 
export class RecipesService{ 
    recipeEmitted=new Subject<Recipe>(); 
    ingredientDeleted=new Subject<Recipe>(); 
    recipesChanged=new Subject<Recipe[]>(); 
private recipes:Recipe[]=[new Recipe('Super Ecd asy Egg Casserole','http://images.media-allrecipes.com/userphotos/560x315/2195255.jpg','Another husband-approved recipe..',[new Ingredient('Apple',10),new Ingredient('Banana',10)]), 
    new Recipe('Super Easy Egg Casserole','http://images.media-allrecipes.com/userphotos/560x315/2195255.jpg','Another husband-approved recipe. Made a couple times recently because of how easy it is to make! This recipe is easy to double or triple, but you may have to cook a bit longer if doing so.',[new Ingredient('Apple',10),new Ingredient('Banana',10)]) 
    ]; 
constructor(private shoppingService:ShoppingListService){} 

    getRecipes(){ 
    return this.recipes.slice(); 
    } 
    getRecipe(id:number){ 
    return this.recipes[id]; 
    } 
    deleteIngredient(recipeId:number,ingredientId:number){ 
    this.recipes[recipeId].ingredients.splice(ingredientId,1); 
    this.ingredientDeleted.next(this.getRecipe(recipeId)); 
    } 
    updateRecipe(id:number,recipe:Recipe){ 
    this.recipes[id]=recipe; 
    this.recipesChanged.next(this.getRecipes()); 
    } 
    addRecipe(recipe:Recipe){ 
    this.recipes.push(recipe); 
    this.recipesChanged.next(this.getRecipes()); 
    } 
addIngredientsToShoppingList(ingredients:Ingredient[]){ 
    this.shoppingService.addRecipeIngredients(ingredients); 
} 
} 

配方列表项

<a style="{cursor:pointer}" [routerLink]="['/recipes',index]" routerLinkActive="active" class="list-group-item clearfix"> 
    <div class="row"> 
    <div class="col-xs-9"> 
     <div class="pull-left"> 
     <h4 class="list-group-item-heading">{{recipe.name}}</h4> 
     <p class="list-group-item-text">{{recipe.description}}</p> 
     </div> 
    </div> 
    <div class="col-xs-3"> 
     <span class="pull-right"> 
     <img src="{{recipe.imageUrl}}" class="img-responsive" style="max-height:50px;"/> 
     </span> 
    </div> 
    </div> 
</a> 
+3

不明。 HTML和代码处理点击的地方在哪里?为什么你首先使用任何索引?配方应该有一个唯一识别它的ID,并允许显示其详细信息。 –

+0

它不需要一个ID,因为整个对象被传递给子组件。 –

+0

@NadunLiyanage除非列表中只包含关于每个配方的一些信息,并且需要根据点击的项目获取细节,这就是问题所暗示的。但在OP澄清之前,我们不能说太多。 –

回答

-1

您应该使用recipe对象而不是索引在click事件中进行处理。由于您已将对象传递给<app-recipes-list-item>组件,因此它将以此方式工作。

+0

为什么要投票?你能评论最新的错误吗? –

+0

我的数组是一个可点击的数组,所以要知道我点击了哪个列表项,我需要知道它的索引。所以虽然我已经通过配方对象,但我需要索引值来编辑配方或显示配方详细信息 – arun

+0

使用EventEmitter类型的@Output()变量并单击组件元素调用'emit(recipe)'并在父组件捕获该事件,并将返回的配方对象与数组与该对象中的唯一变量进行比较。 –