使用 Angular 8 中的 put 调用更新本地 JSON 文件的单个属性

编程入门 行业动态 更新时间:2024-10-10 12:26:50
本文介绍了使用 Angular 8 中的 put 调用更新本地 JSON 文件的单个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想更新我放置在我的资产文件夹中的 JSON 文件,所以如果我只更新我的 JSON 对象的一个​​属性,那么它应该只更新该属性,而不影响任何其他属性值:

让示例代码为:

登录界面.ts

导出接口 loginModel {电子邮件:字符串;密码:字符串;}

登录ponent.ts

import { Component, OnInit } from '@angular/core';从 'rxjs' 导入 { Observable };从@angular/common/http"导入 { HttpClient }从'./loginModel'导入{登录模型}@成分({选择器:'应用程序登录',templateUrl: './loginponent.html',styleUrls: ['./loginponent.css']})导出类 LoginComponent 实现 OnInit {private _jsonURL = 'assets/Login.json';私人登录:Array;构造函数(私有 http: HttpClient) {this.login = new Array();}ngOnInit() {this.getLoginData();}获取登录数据(){this.http.get(this._jsonURL).subscribe(data => {this.login = 数据;console.log(this.login);返回 this.login;});}更新登录数据(){//如何继续这个??}}

loginponent.html

{{log.Email}}<input [ngModel]="log.Password">

<button(点击)="UpdateLoginData()">更新</button>

这只是一个例子.

因此,如果我在一个地方更改密码并单击更新按钮,那么它应该仅更新该特定电子邮件的密码,并且我不想为了更新单个值而用新的 JSON 对象替换整个文件,这可能吗?

解决方案

仅使用 angular 框架无法进行任何文件操作.您需要服务器端实现来实现这一点.如果您不熟悉服务器端编程,您可以尝试使用 in memory angular database api.

https://www.npmjs/package/angular-in-memory-web-api

I want to update my JSON file which I have placed in my assets folder, so If I am updating just one property of my JSON object so it should update that property only, without affecting any other properties value:

Let the sample code be :

loginInterface.ts

export interface loginModel {
    Email: string;
    Password: string;
}

loginponent.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http'
import { loginModel } from './loginModel'

@Component({
  selector: 'app-login',
  templateUrl: './loginponent.html',
  styleUrls: ['./loginponent.css']
})
export class LoginComponent implements OnInit {


  private _jsonURL = 'assets/Login.json';
  private login: Array<loginModel>;

constructor(
    private http: HttpClient) {
    this.login = new Array<loginModel>();
  }
ngOnInit() {
    this.getLoginData();
  }

  getLoginData() {
    this.http.get<loginModel[]>(this._jsonURL).subscribe(data => {
      this.login = data;
      console.log(this.login);
      return this.login;
    });
  }

UpdateLoginData() {
// How to proceed on this one??
  }
}

loginponent.html

<div *ngFor = "let log of login">
    {{log.Email}} 
    <input [ngModel]="log.Password">
</div>
<button (click)="UpdateLoginData()">Update</button>

This is just an example.

So if I am changing password at one place and clicking on update button , then it should update password of that specific Email only and I don't want to replace the whole file with new JSON object just for updating a single value, is this possible?

解决方案

You can't do any file operation using just angular framework. You need server side implementation to achieve this. If you are not familiar with server side programming you can try using in memory angular database api.

https://www.npmjs/package/angular-in-memory-web-api

这篇关于使用 Angular 8 中的 put 调用更新本地 JSON 文件的单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 09:48:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1022363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   文件   Angular   put   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!