1

I trying to run *ngFor with params.

I want to output messages by param, example {{data.lang}}

Thanks.

My code goes like:

-=app.component.html=-

{{lang}}
<div>
  <ul>
    <li *ngFor = "let data of datas">{{data.en}}</li>
  </ul>
</div>

-=app.component.ts=-

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  lang: string = 'en';
  datas: any = [
    {
      "ru": "время деньги.",
      "en": "time is money",
      "fr": null,
      "it": "il tempo è denaro",
      "de": "Zeit Geld",
      "es": "el tiempo es dinero",
      "pt": "tempo é dinheiro",
      "pl": "czas i pieniądze"
    }
  ]
}
Qwertycal
  • 21
  • 4

3 Answers3

3

You Can try like this...

    val(dat){
   return getData[this.lang]
    }
  {{lang}}
<div>
  <ul>
    <li *ngFor = "let data of datas">{{getData(data)}}</li>
  </ul>
</div>
2

In your code you are trying to use *ngFor with an array with only one element. The key is also wrong as data has no "en" element. This is why your output will be empty.

If you want to show information about every key-value in an object you can use Angular Pipes. You can also see this SO answer about it.

AntonioGarcía
  • 1,140
  • 2
  • 15
  • 25
1

In this case you can simply use different notation:

<li *ngFor = "let data of datas">{{data[lang]}}</li>
Zlatko
  • 18,936
  • 14
  • 70
  • 123
  • he wants to print language key for each iteration. so, in effect, he needs to iterate object using `ngFor`. thus > needs pipe or iterate over `Object.keys(data)`... – dee zg May 22 '18 at 06:49
  • Are you certain that's what he wants to do? To me it seems as if he wanted to show the the key per pre-selected language. E.g. if the user picks lang = 'en' somewhere else, this should update and show the 'en' value of each of 'data'. – Zlatko May 22 '18 at 13:36
  • 1
    i am far from certain:) it just sounded that way to me – dee zg May 22 '18 at 14:48