2017-06-21 24 views
0

こんにちは私はこのコンポーネントを私は 'updateUserInfo'サービスから呼び出すが、私は誰でも私を助けることができるビューの値を変更することができないのですか?モデルの角4、更新されたモデルからビューを変更

@Component({ 
    selector: 'app-menu', 
    templateUrl: './menu.component.html', 
    styleUrls: ['./menu.component.css'], 

}); 

export class MenuComponent { 

    firstname = "aaa"; 

    constructor() { } 

    updateUserInfo(user){ 
     this.firstname = "ciao"; 

    } 
} 
+0

何あなたのHTMLが見えますか? – LLai

+0

{{firstname}}

答えて

0

はuser.service.ts

import {Injectable} from '@angular/core'; 
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 
import 'rxjs/add/observable/throw'; 

@Injectable() 
export class UserService { 


    constructor(private http: Http) { 
    } 

getUserName() { 
    let headers = new Headers(); 
    headers.append('X-Requested-With', 'XMLHttpRequest'); 
    headers.append('Content-Type', 'application/json;'); 
    return this.http.get("http://YourUrl",{ headers: headers}) 
         .map((response: Response) =>response.json()) 
    } 
} 

を持っyoгこれはあなたのコンポーネントです:

import { Component, OnInit } from '@angular/core'; 
import { UserService } from '../user.service'; 

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

export class MenuComponent { 

firstname = "aaa"; 

constructor(private userService: UserService) { 
////dont use in constructor many codes, best of use in ngOnInit 
} 

ngOnInit() { 
     this.userService.getUserName() 
     .subscribe(response=> {this.updateUserInfo(response.user.name);}); 
} 

updateUserInfo(user){ 
    this.firstname = user; 
} 
+0

おかげでたくさんの目的プログラムがあります。私は本当にあなたの助けが好きです。 –

+0

私はあなたを助けることができてうれしいです。 – aimprogman

0

あなたは、コントローラ内部の機能を持っている場合は、その関数は、例えば、部品の内部から呼び出されるべきコンストラクタ

+0

問題は、私はあなたがそれを行うことができない場合は、GiovanniPolì@ updateUserInfo() –

+0

を呼び出すサービスを持っていることを私は私が行うことができないと思います – aimprogman

+0

を呼び出すサブスクライブコンポーネントにserviseを使用し、でなければなりませんGiovanniPolì@ updateUserInfo機能 – aimprogman

0
@Component({ 
    selector: 'app-menu', 
    templateUrl: './menu.component.html', 
    styleUrls: ['./menu.component.css'], 
    }) 

export class MenuComponent { 

firstname = "aaa"; 

constructor() { 
    this.updateUserInfo("text"); ///here called function 
} 

updateUserInfo(user){ 
    this.firstname = "ciao"; 
} 

}

0

にupdateUserInfoあなたの関数を呼び出す必要があります。

<div class="profile-usertitle-name"> {{ firstname }} </div> 
<button (click)="updateUserInfo('Mario')">Change name</button> 

サービスを呼び出す場合、サービスには "あなたがそれを購読する「約束」(観察可能なものでもあり得る)。このように:たとえば

constructor(private myService : MyService) { 
    myService.load().subscribe((user) => updateUserInfo(user)); 
} 
+0

問題は、私はupdateUserInfo関数を呼び出すサービスを持っているということです。 –

+0

しかし、コンポーネント内にあるメソッド '.updateUserInfo()'をサービスから呼び出す必要がある場合は、何かが間違っています。通常は、サービスをコンポーネントに挿入する必要があります。おそらくこれはアーキテクチャ上のエラーです。あなたはおそらくその部分を使用していません。 –

関連する問題