Nativescript and ListView, item undefined

2019-09-05 03:35发布

问题:

I m trying to make some experiments with NativeScript, but I m facing some weird error, and I can't get the point.

I m having a simple list of pokemons inside of my component :

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

@Component({
  selector:'pokemon-list',
  templateUrl: 'components/pokemonList/pokemonList.html'
})
export default class PokemonList {

  pokemons: Array<any>;

  constructor(private http:Http){
    this.pokemons = [
      {name: 'Bulbi', url: 'google.com'}
    ];
  }
}

And having the following template associated :

<StackLayout>
  <Label text="Pokemons" class="h1 text-center"></Label>
  <ListView [items]="pokemons">
    <template let-item="pokemon">
      <pokemon-item [name]="pokemon.name" [url]="pokemon.url"></pokemon-item>
    </template>
  </ListView>
</StackLayout>

When I try to start the application, I'm having the following error in my console :

Error in components/pokemonList/pokemonList.html:4:20 caused by: undefined is not an object (evaluating 'self.parentView.context.pokemon.name')

What am I doing wrong ? :/

回答1:

In Angular-2 the syntax for declaring a let in your HTML is the following:

let-myName="item"

in your case:

let-pokemon="item"

At first its kinda strange but it totally make sense. For full lenght example take a look here