Home > Mobile >  Assign a value to a [(ngModel)] input
Assign a value to a [(ngModel)] input

Time:05-17

new to MEAN stack here,

I'm trying to insert datas into MongoDB, but with the _id of the connected user, so I have a function which display it :

  getSessionInfos(){
    return sessionStorage.getItem('id'); 
  }
db.collection('transactions').insertOne({
       date : req.body.date,
       titre: req.body.titre,
       description : req.body.description,
       montant : "-"   req.body.montant,
       id: req.body.id, <-------------
       type: "debit"

So I thought to myself, alright, I'm gonna create a hidden input with the ID as a value, it's gonna be easier :

<input type="text" [(ngModel)]="data.id" value="getSessionInfos()">

Except that NO, it seems that you can't change the value of a NGmodel input, So how am I supposed to do that ?

Thanks a lot

CodePudding user response:

<input [hidden]='true' type="text" [(ngModel)]="data?.id" >

CodePudding user response:

data: any = {
  date: "",
  titre: "",
  description: "",
  montant: "",
  id: sessionStorage.getItem('id'),
  type: ""
}

CodePudding user response:

`<ng-container *ngfor="let dataa in data"><input [hidden]='true' type="text" [(ngModel)]="dataa.id" (change)="getSessionInfos()"></ng-container>`
  • Related