Component:
export class AccountComponent implements OnInit {
public error: any;
currentUser = {};
constructor(private afService: AF) {
afService.getCurrentUserInfo().then(currentUserDetails => {
this.currentUser = currentUserDetails;
});
}
updateUserEntry(fname, lname, phone, bio) {
this.afService.getCurrentUserInfo().then((user) => {
let details = [];
details.push(user);
console.log(details[0].userID);
this.afService.updateUserEntry(details[0].userID, fname, lname, phone, bio).then(() => {
console.log("Updated entry");
})
.catch((error) => {
this.error = error;
console.log("error");
});
});
}
ngOnInit() {
}
}
Form:
<form>
<dl class="form-group">
<dt><label>First Name</label></dt>
<dd><input [(ngModel)]="currentUser.firstName" name="fname" class="form-control" type="text"></dd>
</dl>
<dl class="form-group">
<dt><label>Last Name</label></dt>
<dd><input [(ngModel)]="currentUser.lastName" name="lname" class="form-control" type="text"></dd>
</dl>
<dl class="form-group">
<dt><label>Email</label></dt>
<dd><input [(ngModel)]="currentUser.email" name="email" class="form-control" type="email" disabled></dd>
</dl>
<dl class="form-group">
<dt><label>Mobile Phone</label></dt>
<dd><input [(ngModel)]="currentUser.phone" name="phone" class="form-control" type="text"></dd>
</dl>
<dl class="form-group">
<dt><label>Bio</label></dt>
<dd>
<textarea [(ngModel)]="currentUser.bio" name="bio" class="form-control"></textarea>
</dd>
</dl>
<div class="form-actions">
<button type="button" class="btn btn-primary" (click)="updateUserEntry(fname, lname, phone, bio)">Save changes</button>
</div>
</form>
The function updates a JSON record. For example it updates the "firstName:" entry with the "fname" ngModel. The problem is, the ngModel is read as null unless the user modifies value of the input, even though a value already exists with value="{{ currentUser.firstName }}"
.
How can I set the ngModel to interpret it's value from the actual input's value=""
?
this.fname = this.currentUser.firstName
? (You can take out value). – developer033firstName does not exist on type '{}'
– Joe BerthelotcurrentUser
or atleast set typeany
.public currentUser: any;
– Vamshi