0

I am making an app where you are suppose to be able to edit notes but I am str <textarea class="form-control displayNoteTextAreaD" >{{notesService.ActiveNote.note}}</textarea> - this is displaying the current note I have in the database.

<button (click)="saveNote()" class="button" >Save Note</button>

When I click this button I want to save the new data in a var.

I dont want it to be two way binding.

I have a note: String = "" in my component

This is saveNote()

  saveNote(){
const note =  {
  title: this.notesService.ActiveNote.title,
  note: (the new note),
  type: this.notesService.ActiveNote.type,
  voice: this.notesService.ActiveNote.voice,
  user_id: this.userServices.ActiveUser.id,
  id: this.notesService.ActiveNote.id
}
const _note: Note =  new Note(note);
console.log(_note);
this.notesService.updateUserNote(_note).subscribe(data => {
  console.log(data);
})}
4
  • please show saveNote();
    – dev-dan
    Commented Nov 19, 2018 at 13:57
  • Maybe it's because you're showing the notesService's note, instead of your component's note var, {{component.note}}. On another note, why don't you want to use 2-way data binding? It's a really good way to make your life easier. Commented Nov 19, 2018 at 14:01
  • @GuillermoAguirre The notesServices is the note I want the user to edit. So i want to display that note then after they have edited the note I want to save the new content into a var and then update my database
    – user9623653
    Commented Nov 19, 2018 at 14:03
  • Where are you passing the new note data to the component? Because your function doesn't receive any parameters. There's 2 ways of doing this, with a form with 2-way data binding, but since you don't want 2-way you could have a form that when you click the button executes the submit action. But for what you're trying to do I really recommend 2-way data binding. Commented Nov 19, 2018 at 14:08

1 Answer 1

1

Edit your textarea this way:

<textarea #tarea value="" class="form-control displayNoteTextAreaD" >{{notesService.ActiveNote.note}}</textarea>

Add this to your button:

<button (click)="saveNote(tarea.value)" class="button" >Save Note</button>

your saveNote function should be like this:

  saveNote(noteValue){
const note =  {
  title: this.notesService.ActiveNote.title,
  note: noteValue, // <------
  type: this.notesService.ActiveNote.type,
  voice: this.notesService.ActiveNote.voice,
  user_id: this.userServices.ActiveUser.id,
  id: this.notesService.ActiveNote.id
}
const _note: Note =  new Note(note);
console.log(_note);
this.notesService.updateUserNote(_note).subscribe(data => {
  console.log(data);
})}
2
  • I tried that but it is giving me an error: "ERROR TypeError: Cannot read property 'value' of undefined"
    – user9623653
    Commented Nov 19, 2018 at 14:29
  • I've made a stackblitz and it works fine, please check it out: stackblitz.com/edit/angular-y9hfp5 Commented Nov 19, 2018 at 14:41