Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  59] [ 4]  / answers: 1 / hits: 12805  / 5 Years ago, sun, december 8, 2019, 12:00:00

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;
}


login.component.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: './login.component.html',
styleUrls: ['./login.component.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??
}
}


login.component.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?


More From » angular

 Answers
5

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.com/package/angular-in-memory-web-api


[#5396] Wednesday, December 4, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;