問題描述
如何將日期從頁面轉移"到 Ionic2 中的 side-menu
,即 -具有 app.page.html
如下:
How to "transfer" date from page into side-menu
in Ionic2, i.e -
Having app.page.html
like following:
<ion-menu [content]="content">
<ion-content class="menu-left">
<!-- User profile -->
<div text-center padding-top padding-bottom class="primary-bg menu-left">
<a menuClose>
<h4 light>{{userName}}</h4>
</a>
</div>
<ion-list class="list-full-border">
<button ion-item menuClose *ngFor="let page of pages" (click)="openPage(page)">
<ion-icon item-left name="{{ page.icon }}"></ion-icon>
{{ page.title }}
<ion-badge color="danger" item-right *ngIf="page.count">{{ page.count }}</ion-badge>
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
如何根據從 facebookLogin.page
獲取的數據在頁面中的操作上更新 userName
值?
how to update userName
value upon the action in the page upon the data gotten from facebookLogin.page
?
facebookLogin.page.ts
:
...
fbLogin() {
Facebook.login(["public_profile", "email"])
.then((resp: FacebookLoginResponse) => {
if (resp.status === "connected") {
Facebook.api("/me", ["public_profile", "email"])
.then(res => {
this.userName = res.name
this.login({name: this.userName})
})
...
);
}
login(params) {
this.nav.setRoot(HomePage, params);
}
...
(那么,我如何將使用 facebook 登錄后獲得的 userName
導入 app.html
中的 ion-sideMenu
中;如何我可以讓 EventEmmiter、service/observe 來觀察 app.html 的 ion-menu 的變化……其他方式?)
(so, how would I import the userName
which I get after login with facebook into ion-sideMenu
in app.html
; how I could make EventEmmiter, service/observe for changes in app.html's ion-menu ... some other way?)
推薦答案
ionic2 - 使用事件
查看活動文檔
它們允許您從任何頁面發布"操作,并在另一個頁面中訂閱它以檢索值.你的場景看起來有點像這樣.
They allow you to 'publish' an action from any page, and subscribe to it in another page to retrieve the value. Your scenario would look a bit like this.
導入(在 Facebooklogin.component
和 app.component
上)
Import (both on Facebooklogin.component
and app.component
)
import { Events } from 'ionic-angular';
和你的構造函數 constructor(public events: Events)
然后,每當您更改 userName
(例如在 facebook 登錄的處理程序中)時,都會像這樣發布值.
Then, whenever you change your userName
(f.e. in the handler of the facebook login) publish the value like this.
fbLogin() {
Facebook.login(["public_profile", "email"])
.then((resp: FacebookLoginResponse) => {
if (resp.status === "connected") {
Facebook.api("/me", ["public_profile", "email"])
.then(res => {
this.userName = res.name
// publish the username change to the events
this.events.publish('username:changed', this.userName);
this.login({name: this.userName})
})
//...
);
}
并訂閱在您的 app.component
中進行的任何發布
And subscribe to any publishes being made in your app.component
userName: string;
constructor(events: Events) {
this.userName = "not logged in";
events.subscribe('username:changed', username => {
if(username !== undefined && username !== ""){
this.userName = username;
}
}) //...
}
<小時>
angular2 - 使用 EventEmitter
angular2 - using EventEmitter
import { EventEmitter } from '@angular/core';
public userChanged = new EventEmitter();
fbLogin() {
Facebook.login(["public_profile", "email"])
.then((resp: FacebookLoginResponse) => {
if (resp.status === "connected") {
Facebook.api("/me", ["public_profile", "email"])
.then(res => {
this.userName = res.name
// publish the username change to the events
this.userChanged.emit(this.userName);
this.login({name: this.userName})
})
...
);
}
App.component
App.component
import { FacebookLogin } from '../pages/facebook/facebook.component';
public userName;
constructor(fb: FacebookLogin){
this.userName = "";
//subscribe to the page's EventEmitter
this.fb.userChanged.subscribe(username => {
this.userName = username;
});
}
或使用 EventEmitter
作為 Output
,如本 S.O. 中所述.回答:EventEmitter 的正確用法是什么?
OR use the EventEmitter
as an Output
as described in this S.O. answer: What is the proper use of an EventEmitter?
這篇關于如何更新 ionic 2 側菜單中的值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!