2017-12-15 20 views
3

私は単純にサービスを作成し、コンポーネントに登録しました。しかし、サービス "ngFor"からデータを取得するのは機能しません。これは私のコードであるngForはHTML要素にバインドしていません

Property binding ngforFor not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".

、これは私のサービスである

import { Component, Input, OnInit } from '@angular/core'; 

import { UserService } from '../user.service'; 

@Component({ 
    selector: 'app-tutorial', 
    template: `<p>tutorial works!</p> 
      <p> {{userName | uppercase}} </p> 
      <ul *ngfor="let user for users"> 
       <li>{{user.name}}</li> 

      </ul> 
     ` 
    }) 
export class TutorialComponent implements OnInit { 
    @Input() 
    public userName:String; 

    users=[]; 

    constructor(private _userService: UserService){ } 

    ngOnInit(){ 
    this.users=this._userService.getUsers(); 
    } 


} 

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

@Injectable() 
export class UserService { 
getUsers(){ 
    return[ 
    {'id':1,'name':'Jyoti','password':'123'} 
    ]; 
    } 


} 

そして、ここでappcomponentで私が登録したサービス、

: それは次のエラーを示しています
import { Component } from '@angular/core'; 
import {TutorialComponent} from './tutorial/tutorial.component'; 
import { UserService } from './user.service'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
    providers:[UserService] 
}) 
export class AppComponent { 

    title = 'My Angular2 Login App'; 
    public msg1:string; 
    public uname; 
    public psw; 
    onClick (value1,value2) { 
    console.log('Username: '+value1+', password:' +value2); 
    } 

} 

解決策を提案してください。

+0

です。サブスクライブ後、ユーザー配列オブジェクトに値を割り当てます。 – Vignesh

答えて

2

問題は、マップと値をサブスクライブする必要があるため、 "* ngfor" 使用 "* ngFor"

<ul *ngFor="let user for users"> 
      <li>{{user.name}}</li> 

    </ul> 
関連する問題