无法在角度函数内打印变量值

时间:2017-08-04 01:38:48

标签: angular

我是Angular的新手,我编写了以下代码,但这段代码似乎对我不起作用。

HTML:

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

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

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

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

</form>

.TS

addArticle(title : HTMLInputElement, link : HTMLInputElement):boolean{
    console.log('Adding article with title: ${title.value} and link ${link.value}');
    return false;
  }

当我运行代码时,我在控制台的输出下面,不知道我做错了什么:

Adding article with title: ${title.value} and link ${link.value}

2 个答案:

答案 0 :(得分:1)

我认为你使用的是单引号(')而不是反引号(`),试试下面的代码,它应该可以工作

addArticle(title : HTMLInputElement, link : HTMLInputElement):boolean{
    console.log(`Adding article with title: ${title.value} and link ${link.value}`);
    return false;
  }

答案 1 :(得分:0)

只需用反引号替换单引号