|
@@ -1,52 +1,115 @@
|
|
|
+// import { Component, OnInit } from '@angular/core';
|
|
|
+// import { AlertController } from '@ionic/angular';
|
|
|
+// @Component({
|
|
|
+// selector: 'app-memo',
|
|
|
+// templateUrl: './memo.page.html',
|
|
|
+// styleUrls: ['./memo.page.scss'],
|
|
|
+// })
|
|
|
+// export class MemoPage implements OnInit {
|
|
|
+// segment: string = 'memo';
|
|
|
+// memos: string[] = [];
|
|
|
+// newMemo: string = '';
|
|
|
+// newMessage: string = '';
|
|
|
+// constructor(private alertController: AlertController) {}
|
|
|
+// ngOnInit() {
|
|
|
+// this.loadMemos();
|
|
|
+// }
|
|
|
+// //加载备忘录
|
|
|
+// loadMemos() {
|
|
|
+// const memos = localStorage.getItem('memos');
|
|
|
+// if (memos) {
|
|
|
+// this.memos = JSON.parse(memos);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// //保存备忘录
|
|
|
+// saveMemos() {
|
|
|
+// localStorage.setItem('memos', JSON.stringify(this.memos));
|
|
|
+// }
|
|
|
+// //异步函数,添加备忘录
|
|
|
+// async addMemo() {
|
|
|
+// if (this.newMemo.trim().length > 0) {
|
|
|
+// this.memos.push(this.newMemo);
|
|
|
+// this.newMemo = '';
|
|
|
+// this.saveMemos();
|
|
|
+// const alert = await this.alertController.create({
|
|
|
+// header: '提示',
|
|
|
+// message: '添加成功',
|
|
|
+// buttons: ['确定']
|
|
|
+// });
|
|
|
+// await alert.present();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// //异步函数,确认删除
|
|
|
+// async confirmDeleteMemo(memo: string) {
|
|
|
+// const alert = await this.alertController.create({
|
|
|
+// header: '确认删除',
|
|
|
+// message: `你确定要删除 "${memo}" 吗?`,
|
|
|
+// buttons: [
|
|
|
+// {
|
|
|
+// text: '取消',
|
|
|
+// role: 'cancel'
|
|
|
+// },
|
|
|
+// {
|
|
|
+// text: '删除',
|
|
|
+// handler: () => {
|
|
|
+// this.memos = this.memos.filter(m => m !== memo);
|
|
|
+// this.saveMemos();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// ]
|
|
|
+// });
|
|
|
+// await alert.present();
|
|
|
+// }
|
|
|
+// //删除备忘录
|
|
|
+// deleteMemo(memo: string) {
|
|
|
+// this.memos = this.memos.filter(m => m !== memo);
|
|
|
+// this.saveMemos();
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { AlertController } from '@ionic/angular';
|
|
|
|
|
|
+interface Memo {
|
|
|
+ content: string;
|
|
|
+ timestamp: Date;
|
|
|
+}
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-memo',
|
|
|
templateUrl: './memo.page.html',
|
|
|
styleUrls: ['./memo.page.scss'],
|
|
|
})
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
export class MemoPage implements OnInit {
|
|
|
segment: string = 'memo';
|
|
|
- memos: string[] = [];
|
|
|
+ memos: Memo[] = [];
|
|
|
newMemo: string = '';
|
|
|
- newMessage: string = '';
|
|
|
+
|
|
|
constructor(private alertController: AlertController) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
this.loadMemos();
|
|
|
}
|
|
|
- //加载备忘录
|
|
|
+
|
|
|
loadMemos() {
|
|
|
const memos = localStorage.getItem('memos');
|
|
|
if (memos) {
|
|
|
this.memos = JSON.parse(memos);
|
|
|
}
|
|
|
}
|
|
|
- //保存备忘录
|
|
|
+
|
|
|
saveMemos() {
|
|
|
localStorage.setItem('memos', JSON.stringify(this.memos));
|
|
|
}
|
|
|
|
|
|
- //获取时间戳
|
|
|
- getCurrentTimestamp(): string {
|
|
|
- const now = new Date();
|
|
|
- const year = now.getFullYear();
|
|
|
- const month = (now.getMonth() + 1).toString().padStart(2, '0');
|
|
|
- const day = now.getDate().toString().padStart(2, '0');
|
|
|
- const hours = now.getHours().toString().padStart(2, '0');
|
|
|
- const minutes = now.getMinutes().toString().padStart(2, '0');
|
|
|
- return `${year}年${month}月${day}日${hours}:${minutes}`;
|
|
|
- }
|
|
|
-
|
|
|
- //异步函数,添加备忘录
|
|
|
async addMemo() {
|
|
|
if (this.newMemo.trim().length > 0) {
|
|
|
- this.memos.push(this.newMemo);
|
|
|
+ const newMemoObj: Memo = {
|
|
|
+ content: this.newMemo,
|
|
|
+ timestamp: new Date(),
|
|
|
+ };
|
|
|
+ this.memos.push(newMemoObj);
|
|
|
this.newMemo = '';
|
|
|
this.saveMemos();
|
|
|
const alert = await this.alertController.create({
|
|
@@ -57,11 +120,11 @@ export class MemoPage implements OnInit {
|
|
|
await alert.present();
|
|
|
}
|
|
|
}
|
|
|
- //异步函数,确认删除
|
|
|
- async confirmDeleteMemo(memo: string) {
|
|
|
+
|
|
|
+ async confirmDeleteMemo(memo: Memo) {
|
|
|
const alert = await this.alertController.create({
|
|
|
header: '确认删除',
|
|
|
- message: `你确定要删除 "${memo}" 吗?`,
|
|
|
+ message: `你确定要删除 "${memo.content}" 吗?`,
|
|
|
buttons: [
|
|
|
{
|
|
|
text: '取消',
|
|
@@ -70,8 +133,7 @@ export class MemoPage implements OnInit {
|
|
|
{
|
|
|
text: '删除',
|
|
|
handler: () => {
|
|
|
- this.memos = this.memos.filter(m => m !== memo);
|
|
|
- this.saveMemos();
|
|
|
+ this.deleteMemo(memo);
|
|
|
}
|
|
|
}
|
|
|
]
|
|
@@ -79,10 +141,8 @@ export class MemoPage implements OnInit {
|
|
|
await alert.present();
|
|
|
}
|
|
|
|
|
|
- //删除备忘录
|
|
|
- deleteMemo(memo: string) {
|
|
|
+ deleteMemo(memo: Memo) {
|
|
|
this.memos = this.memos.filter(m => m !== memo);
|
|
|
this.saveMemos();
|
|
|
}
|
|
|
-
|
|
|
}
|