如何在不刷新页面的情况下查看新条目?

问题描述 投票:-3回答:1

在我的page.html中使用ngIf,使用以下代码从本地存储中获取数据:

<!--
  Generated template for the EducationPage page.

  See http://ionicframework.com/docs/components/#navigation for more info on
  Ionic pages and navigation.
-->
<ion-header>
  <ion-navbar>
    <button ion-button menuToggle>
        <ion-icon name="menu"></ion-icon>
      </button>
    <ion-title>Education</ion-title>
  </ion-navbar>
</ion-header>
<ion-content padding>
  <ion-item>
    <ion-input [(ngModel)]="newentry.title" type="text" placeholder="Title" [value]="title ? title: ''">
    </ion-input>
  </ion-item>
  <ion-item>
    <ion-input [(ngModel)]="newentry.description" type="text" placeholder="Description">
    </ion-input>
  </ion-item>
  <ion-item>
    <ion-input [(ngModel)]="newentry.tagline" type="text" placeholder="Tag line">
    </ion-input>
  </ion-item>
  <ion-item>
    <ion-input [(ngModel)]="newentry.date" type="text" placeholder="Date">
    </ion-input>
  </ion-item>
  <button ion-button item-right>
       <ion-icon name="md-add-circle" (click)="save(newentry);"></ion-icon>
      <!--  <ion-icon name="md-add-circle" (click)="editmode ? save() : editedu(elem, index)"></ion-icon> -->
      </button>
  <ul id="elements">
    <li *ngFor="let elem of fetchdata; let index = index">
      {{elem.title}} {{elem.description}}
      <button ion-button item-right (click)="editedu(elem,index);">
                <ion-icon name="open"></ion-icon>
              </button>
      <button ion-button item-right (click)="deleteedu(elem,index)">
                  <ion-icon name="trash"></ion-icon>                 
      </button>
    </li>
  </ul>

</ion-content>

在同一页面上,我有一个添加新数据的表单,但是当我添加和保存时,我必须再次从菜单加载页面以查看输入的新数据,如何立即加载?

这是我页面的外观:

enter image description here

更新

发布我的page.ts代码:

import { Component } from '@angular/core';
import {  NavController, NavParams } from 'ionic-angular';
import { Education } from "./education";

/**
 * Generated class for the EducationPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
//@IonicPage()
@Component({
  selector: 'page-education',
  templateUrl: 'education.html',
})




export class EducationPage {

  list: Array<number> = [1, 2, 3];
  //myArray =  [{title:"saurabh" , description:"dd" , tagline:"tt", date:"dd"},{title:"gaurav" , description:"dd" , tagline:"tt", date:"dd"}];
  myArray = [];
  number1 = 5;
  newentry = { title: "", description: "", tagline: "", date: "", id: Math.random().toString(36).substr(2, 10) };
  data = '';
  fetchdata = [];
  dataObj: Education[] = [];
  elem = '';
  index;
  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }



  ionViewDidLoad() {
    console.log('ionViewDidLoad EducationPage');
    //console.log(this.list);
    // console.log(this.myArray);
    //  localStorage.setItem("education-number",JSON.stringify(this.list));
    this.data = localStorage.getItem('education');
    if (!this.data) {
      localStorage.setItem("education", JSON.stringify(this.myArray));
    }
    this.fetchdata = JSON.parse(localStorage.getItem('education'));
    console.log(this.fetchdata);

  }

  // updateeditmode(){
  //   this.editmode=true;
  //   console.log(this.editmode);
  // }

  editedu(elem, index) {
    // console.log(elem); 
    console.log("inside editedu::");
    this.newentry = elem;
    this.newentry.title = elem.title;
    // this.fetchdata.splice(index, 1);
    //elem.title = 'CLICKED';
    console.log(this.fetchdata);
    localStorage.setItem("education", JSON.stringify(this.fetchdata));
  }

  deleteedu(elem, index) {
    console.log(elem);
    console.log(index);
    this.fetchdata.splice(index, 1);
    //elem.title = 'CLICKED';
    console.log(this.fetchdata);
    localStorage.setItem("education", JSON.stringify(this.fetchdata));
  }



  save(entry: Education) {
    this.data = localStorage.getItem('education');
    console.log("save working");
    console.log(this.data);
    if (this.data) //check if it exists or not empty
    {
      console.log("inside save function");
      this.dataObj = JSON.parse(this.data); //parse string
      console.log(this.dataObj);   
      let filtered = this.dataObj.filter(t => t.id === entry.id);
      if (filtered.length > 0) {
        this.dataObj[this.dataObj.findIndex(eel=>eel.id === entry.id)] = entry;
      } else {
        this.dataObj.push(this.newentry);
      }
      console.log(this.dataObj);
      localStorage.setItem("education", JSON.stringify(this.dataObj));
    }



    console.log("inside save edit area");
    //this.editedu(this.elem,this.index);
  }

}
angular typescript angular2-template
1个回答
0
投票

尝试以下一项:

ApplicationRef.tick()-类似于Angular 1的$ rootScope。$ digest()-即检查整个组件树

ChangeDetectorRef.detectChanges()-类似于$ scope。$ digest()-即,仅检查此组件及其子组件

从订阅方法更新数据后,请使用其中之一来更新具有新更改的视图。


0
投票

尝试以下一项:

ApplicationRef.tick()-类似于Angular 1的$ rootScope。$ digest()-即检查整个组件树

ChangeDetectorRef.detectChanges()-类似于$ scope。$ digest()-即,仅检查此组件及其子组件

从订阅方法更新数据后,请使用其中之一来更新具有新更改的视图。

© www.soinside.com 2019 - 2024. All rights reserved.