警报或动作表后重新加载页面弹出离子2

时间:2017-10-04 11:26:17

标签: ionic2 page-refresh

我在运行时使用操作表警告弹出窗口中的输入字段更新数据。为此,我创建了服务编辑用户 - 提供商 我删除了以前的数据。删除后,我在存储中使用设置新数据 警报中的输入字段。

更换数据

this.storage.remove(entry.Username).then(() => //Removing data
{
    data["Password"] = entry.Password;
    delete data["verifyPass"];
    this.storage.set(data.Username, data).then(() => //Setting data
    {
        this.flashMsg.flashMessage(success);
    }).catch(() =>
    {
        this.flashMsg.flashMessage("Some error occurred!");
    });
});

这是我用来存储数据的方法。

changeUserOrDisp(entry)
{
    let changeDispUser = this.alert.create(
    {
        title: "Edit Account",
        subTitle: "Change display name or username or leave the field blank if you don\'t want to change",
        mode: "ios",
        inputs:
        [{type: "password", placeholder: "Enter Password", name: "verifyPass"},
        {type: "text", placeholder: "Enter new display name", name: "Dispname"},
        {type: "text", placeholder: "Enter new username", name: "Username"}],
        buttons:
        [{
            text: "OK",
            handler: (data) =>
            {
                if(data.verifyPass == entry.Password)
                {
                    if(data.Username !== "")
                    {
                        this.isNotDuplicate(data.Username).then((success) =>
                        {
                            if(data.Dispname !== "")
                            {
                                this.storage.remove(entry.Username).then(() => //Here, removing previous data
                                {
                                    data["Password"] = entry.Password;
                                    delete data["verifyPass"];
                                    this.storage.set(data.Username, data).then(() =>
                                    {//Here, Storing new data
                                        this.flashMsg.flashMessage(success);
                                    }).catch(() =>
                                    {
                                        this.flashMsg.flashMessage("Some error occurred!");
                                    });
                                });
                            }
                        }).catch((error) =>
                        {
                            this.flashMsg.flashMessage(error);
                        });
                    }
                }
                else
                    this.flashMsg.flashMessage("Wrong Password");
            }
        },
        {
            text: "Cancel",
            role: "cancel"
        }]
    });
    changeDispUser.present();
}

保存数据后,我想更新rootPage,即 AdminPage。 问题是,由于生命周期挂钩 ionViewDidLoad(), showData()只会执行一次。

此处 showUsers()方法(文件: Admin-Page.ts )。

showUsers()
{
    this.storage.ready().then(() =>
    {
        this.storage.forEach((data, key, iterator) =>
        {
            if(data.Username !== "admin")
                this.usersData.push(data);
        });
    });
}

我在 ionViewDidLoad()中调用它。(文件: Admin-Page.ts

ionViewDidLoad()
{
    this.menuCtrl.enable(true, "menu");
    this.menuItems = 
    [
        {title: "log Out", icon: "log-out", method: () => this.logOut()}
    ]
    this.showUsers();
    this.adminData = this.navParams.data;
}

在popUps之后我该怎么做才能再次执行 showData()?即我的数据得到更新。我尝试了 ionViewDidEnter(),但这也没有用。

此处有整个文件 Admin-Page.ts

import { Component } from '@angular/core';
import { SignUpPage } from "../sign-up/sign-up";
import { FlashMessageProvider } from "../../providers/flash-message/flash-message";
import { Storage } from '@ionic/Storage';
import { EditUsersProvider } from "../../providers/edit-users/edit-users";
import { UserAccountPage } from "../user-account/user-account";
import {
    IonicPage,
    NavController,
    NavParams,
    ActionSheetController,
    AlertController,
    ModalController, 
    MenuController} from 'ionic-angular';
import { HomePage } from "../home/home";

@IonicPage()
@Component(
{
    selector: 'page-admin',
    templateUrl: 'admin.html',
})
export class AdminPage
{
    menuItems: Array<{title: string, icon: string, method: () => void}>;
    usersData = [];
    adminData = {Dispname: "", Username: "", Password: ""};
    constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private flashMsg: FlashMessageProvider,
    public storage: Storage,
    public editPanel: ActionSheetController,
    public alert: AlertController,
    private editUsers: EditUsersProvider,
    private modalCtrl: ModalController,
    public menuCtrl: MenuController) {}
    ionViewDidLoad()
    {
        this.menuCtrl.enable(true, "menu");
        this.menuItems = 
        [
            {title: "log Out", icon: "log-out", method: () => this.logOut()}
        ]
        this.showUsers();
        this.adminData = this.navParams.data;
    }
    showUsers()
    {
        this.storage.ready().then(() =>
        {
            this.storage.forEach((data, key, iterator) =>
            {
                if(data.Username !== "admin")
                    this.usersData.push(data);//<--data to show in template
            });
        });
    }
}

这是模板文件 Admin-Page.html。

<ion-header>
    <ion-navbar>
        <ion-title>Admin</ion-title>
        <ion-buttons end>
            <button icon-only ion-button (tap) = "addUser()"><ion-icon name = "add"></ion-icon></button>
        </ion-buttons>
        <ion-buttons left>
            <button ion-button icon-only menuToggle><ion-icon name = "menu"></ion-icon></button>
        </ion-buttons>
    </ion-navbar>
</ion-header>
<ion-content padding>
    <h1 align = "center">
        Welcome Admin
    </h1>
    <ion-list>
        <ion-item *ngFor = "let data of usersData" (press) = "edit(data)" (tap) = "modalUsers(data)">
                {{data.Dispname}}
        </ion-item>
    </ion-list>
</ion-content>
<ion-nav #content id = "content" swpieBackEnabled = "false"></ion-nav>

1 个答案:

答案 0 :(得分:0)

您可以使用Events API发布和订阅保存事件,并调用所需的方法来检索您的数据。

在提供商的构造函数中,注入事件,

constructor(private events:Events){}

在保存功能中,

this.storage.set(data.Username, data)
  .then(() =>
      {//Here, Storing new data
       this.flashMsg.flashMessage(success);
       this.events.publish("Data Saved",data);                              
       }).catch(() =>{
            this.flashMsg.flashMessage("Some error occurred!");
          });

在管理页面中注入相同的事件。 在ionViewDidLoad()中,订阅该活动,

ionViewDidLoad()
{
    this.menuCtrl.enable(true, "menu");
    this.menuItems = 
    [
        {title: "log Out", icon: "log-out", method: () => this.logOut()}
    ]
    this.showUsers();
    this.events.subscribe('Data Saved',data => {
        this.showUsers();
        console.log(data);
    });
    this.adminData = this.navParams.data;
}