javascript - Angular 2模板无法识别模板引用变量

标签 javascript angular templates

我正在使用 Angular 2 构建一个网站,但在模板引用变量方面遇到了一些问题。我正在创建一个非常简单的表单来将新列表添加到在线商店:

<div class="row">
<div class="col-sm-6">
  <div class="form-group">
    <label for="productSKU">Product SKU</label>
    <input #productSKU type="text" class="form-control" id="productSKU" placeholder="Enter Product SKU">
  </div>
  <div class="form-group">
    <label for="productTitle">Product Title</label>
    <input type="text" class="form-control" id="productTitle" placeholder="Enter Product Title" #productTitle>
  </div>
  <div class="form-group">
    <label for="productSubtitle">Product Subtitle</label>
    <input type="text" class="form-control" id="productSubtitle" placeholder="Enter Product Subtitle" #productSubtitle>
  </div>
  <div class="form-group">
    <label for="productPrice">Product Price (USD)</label>
    <input type="text" class="form-control" id="productPrice" placeholder="Enter Product Price" #productPrice>
  </div>
  <div class="form-group">
    <label for="productType">Select Product Type</label>
    <select multiple class="form-control" id="productType" #productType>
      <option *ngFor="let type of listingTypes" [value]="type">{{type}}</option>
    </select>
  </div>
</div>
<div class="col-sm-6">
  <div class="form-group">
    <label for="productDescription">Product Description</label>
    <textarea class="form-control" id="productDescription" rows="8" #productDescription></textarea>
  </div>
  <div class="form-group">
    <label for="productCondition">Product Condition</label>
    <input type="text" class="form-control" id="productCondition" placeholder="Enter Product Condition" #productCondition>
  </div>
  <div class="form-group">
    <label for="storageLocation">Storage Location</label>
    <input class="form-control" id="storageLocation" placeholder="Enter Storage Location" #storageLocation>
  </div>
  <div class="form-group">
    <label for="image1Path">Image 1</label>
    <input type="text" class="form-control" name="image1Path" id="image1Path" placeholder="Enter Image 1 File Name" #image1Path>
  </div>
  <button class="btn" id="newPostSubmitButton" (click)="onNewListingSubmit(productTitle.value,
                                                                            productSubtitle.value,
                                                                            productType.value,
                                                                            productPrice.value,
                                                                            productDescription.value,
                                                                            productCondition.value,
                                                                            productSKU.value,
                                                                            storageLocation.value,
                                                                            image1Path.value)">Submit</button>
</div>

出于某种原因,当我尝试在单击提交按钮时触发组件中的 onNewListingSubmit 方法时,出现以下错误:

error example

如您所见,它找不到 undefined 的属性“value”。它似乎无法识别整个表单中的各种模板引用变量(例如#productSKU、#productPrice 等)

有什么想法可能会发生这种情况吗?似乎找不到同一问题的任何其他示例。谢谢

最佳答案

看起来 productCondition 未在模板上定义。

关于javascript - Angular 2模板无法识别模板引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044872/

相关文章:

C++ 在编译时比较模板类型

javascript - 模仿 Javascript escape() 函数

javascript - 在 Javascript 中从字符串中删除给定值以及逗号

javascript - 我可以用 jquery 触发 Html.ValidationMessageFor 并让它显示我在 JQuery 中定义的一些字符串吗?

unit-testing - 使用原生 View 封装时访问嵌套组件的DebugElement

typescript - 为什么我的模板在 angular2 组件中不起作用?

c++ - 是否可以使用带有模板化参数的特征 block 表达式作为左值?

javascript - Yii2:在单击按钮时更改 Gridviews 的 DataProvider

css - 无法更改 bootstrap-datepicker 的颜色

templates - 查找运算符以包含 Handlebars 上的部分内容