2

I'm working with angular 7. I'm trying to retrieve the change of a variable value of the template component. Indeed, I declare a variable of boolean type into my .ts file and into the template file its value change after clicking on a button. I want to display this new value on the console. How can I do it please.

I tried double way binding but I'm not working with 2 components but only one and I saw that il works with @Input and @Output so I think it's not possible for me.

5
  • on the onClick() of the button you can change the value and also console it Commented May 2, 2019 at 14:53
  • 1
    show all your code and it may clarify what you're after here. Commented May 2, 2019 at 14:56
  • You can see the value in the template by adding <pre>{{value | json}}<pre> which is sometimes easier then trying to log it. Commented May 2, 2019 at 14:56
  • Thank you guys ! My problem was that I didn't use a function for the event binding but just a syntax like : <button (click)='state=!state'>Appuyer</button>. So it was difficult for me to display the "state" variable into the console as I tried to display this variable into the constructor of my .ts file. Commented May 2, 2019 at 15:13
  • so maybe you want to delete this question? or self-answer it ? Commented May 2, 2019 at 21:53

1 Answer 1

0

make a function in your ts:

changeValue(){
  myBool = !myBool;
  console.log(myBool);
}

in your html, add (click)="changeValue()" to your button tag.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ! My problem was that I didn't use a function for the event binding but just a syntax like : <button (click)='state=!state'>Appuyer</button>. So it was difficult for me to display the "state" variable into the console as I tried to display this variable into the constructor of my .ts file.
There is a check mark to the answer's left, click it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.