簡體   English   中英

TypeError:無法讀取Angular 4中未定義的屬性“值”

[英]TypeError: Cannot read property 'value' of undefined in Angular 4

我遇到了Angular 4(Angular-CLI)中的錯誤,它說: TypeError: Cannot read property 'value' of undefined 因此,我是來自Angular 1的Angular 4的新用戶。因此,我將感謝您的幫助。 提前致謝。

這是我的代碼。

app.component.html

<!-- using semantic-ui --> 
<form class="ui large form segment">
  <h3 class="ui header">Add a link</h3>
  <!-- added the title -->

  <div class="field">
    <label for="title">Title:</label>
    <input name="title" #newtitle>
  </div>
  <!-- added the link -->
  <div class="field">
    <label for="link">Link:</label>
    <input name="link" #newLink>
  </div>
  <!--added the button -->

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

</form>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {

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

您引用變量的大小寫與傳遞的參數不匹配。 <input name="title" #newtitle>更改為<input name="title" #newTitle>因為您正在addArticle帶有newTitle T的newTitle調用addArticle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM