0

I have one array in angular and one form in which I want to replace placeholder's value from that array.

check below code :

fieldType = [{
   id: 1, 
   defaultData: {
      inputPlacehoolder: 'enter Text'
   }
 },{
   id: 2, 
   defaultData: {
      inputPlacehoolder: 'enter Email'
   }
 },{
   id: 3, 
   defaultData: {
      inputPlacehoolder: 'enter Value'
   }
}]

fieldArray = [
   {
      type: 1,
      value: "demo"
   },
   {
      type: 2,
      value: "[email protected]"
   }
]

and Here is html code :

<form *ngFor="let field of fieldArray">
    <input type="text" placeholder="{{ fieldType | filterPipe: field.type : 'inputPlacehoolder' }}" />
</form>

and here is my pipe like this :

transformPipe(fieldType, id, key) {
    return fieldType.find(item => item.id == id).defaultData[key]
}

expected output should like this :

<input type="text" placeholder="enter Text" />
<input type="text" placeholder="enter Email" />

I want to filter array and show only one string in html using angular pipe. but this pipe return me error like : Multiple components match node with tagname

can anyone please suggest me how to filter array and show only single string in angular ?

I can't call function in that placeholder binding because it causes performance issues not even merge that two array for show direct value using loop.

Pipe is the only way. please help me to figure it out...

2
  • do you really have let before fieldArray in your component? Commented Mar 5, 2021 at 12:52
  • Nope, There is a some typing mistake. uff.... Coder's default habit.... Commented Mar 8, 2021 at 4:29

1 Answer 1

1

You can try this code:

export class FilterPipe implements PipeTransform {
  transform(fieldType: any[], id: any, key: any): any {
    let placeholder =   fieldType.filter(r => r.id == id).length>0? fieldType.filter(r => r.id == id)[0].defaultData
      .inputPlacehoolder:"";

    return placeholder;
  }
}

for more view this code: https://stackblitz.com/edit/angular-ivy-mis3fx?file=src%2Fapp%2FfilterPipe.ts

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply. It's working for me. I found one more solution : that I switch my passed argument in pipe and it's getting worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.