這似乎是一個非常簡單的問題,但我是Angular2的新手,我對組件之間的關係有點困惑。我有一個登錄頁面,當用戶輸入他們的用戶名時,我想要在儀表板上顯示用戶名,例如「Welcome,James」。顯示登錄用戶的用戶名(Angular 2)
我有一個login.html
頁面,loginHTMLcomponent
有一個templateurl
到login.html
頁面及其父login.ts。 我有dashboard.ts
這是dashboard.component.ts
父母這(對儀表盤頁面的HTML)
基本上我如何才能從輸入字段的用戶名中的login.html出現在dashboard.component.html ...我可能已經很好地解釋過了。謝謝。
我會盡力解釋更清楚一點 在我的login.html我有類似:
input type = "text" [(ngModel="name")]
而且在我dashboard.component.ts,在裏面我的選擇我會:
@Component({
selector: 'dashboardHTML',
template:
`
<body>
........
..........
<span ="user">Welcome, {{user}}</span>
...
`
更新: 只是想知道是接近使用服務的正確方法。 在我login.component.html
我:
<input id="username" type="text" class="form-control"
placeholder="Username" ngControl="username" [(ngModel)]="name">
在我login.HTMLComponent
(其中有一個templateURL到login.component.html
)我有export class LoginHTMLComponent{ public name: {}; /////
在我name.service.ts
我有這個
import { Injectable } from '@angular/core';
import {LoginHTMLComponent} from "./LoginComponents/login.HTMLcomponent";
@Injectable()
export class NameService {
getName(){
return name;
}
}
我米不知道我是否在這裏正確調用名稱。 謝謝
非常感謝您的幫助 – Edmond
我投的好,但我沒有足夠的帖子來顯示,如果你有機會,你能看看我對我的帖子所做的修改嗎?謝謝 – Edmond