0

I am new to angular and cannot figure out how to log in console the value of 2 variables. As the result I get the undefined values of the variables. Why?

My html:

<form class="ui large form segment">
<h3 class="ui header">Add a link</h3>

<div class="field">
    <label for="title" #newtitle>Title</label>
    <input name="title">
</div>
<div class="field">
    <label for="link" #newlink>Link:</label>
    <input name="link">
</div>

<button (click)="addArticle(newtitle, newlink)" class="ui positive right floated button">
    Submit Link
</button>

.comonent.ts:

addArticle(title: HTMLInputElement, link: HTMLInputElement): boolean{
    console.log(`Adding article title: ${title.value} and link: ${link.value}` );
    return false;
}
2
  • Ate you sure the elements have values? Commented May 22, 2017 at 17:59
  • I will update the code now Commented May 22, 2017 at 18:00

1 Answer 1

1

Move the template variables from the labels to the inputs themselves:

<form class="ui large form segment">
<h3 class="ui header">Add a link</h3>

<div class="field">
    <label for="title">Title</label>
    <input #newtitle name="title">
</div>
<div class="field">
    <label for="link">Link:</label>
    <input #newlink name="link">
</div>

<button (click)="addArticle(newtitle, newlink)" class="ui positive right floated button">
    Submit Link
</button>
Sign up to request clarification or add additional context in comments.

Comments

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.