简体   繁体   English

无法将表格数据从html(view)传递到angular2中的相关组件

[英]Not able to pass form data from html(view) to it's associated component in angular2

Error: 错误:

EXCEPTION: Uncaught (in promise): Error: Error in app/search/search.component.html:10:35 caused by: Cannot read property 'searchFor' of undefined 例外:未捕获(承诺):错误:app / search / search.search.component.html:10:3​​5中的错误,原因是:无法读取未定义的属性'searchFor'

I have this html file from which I need to create a object called 'chemical' which will have two properties: 1)searchFor (coming from text input) 2) searchBy (coming from dropdown menu) as shown below: 我有这个html文件,需要从中创建一个名为“ chemical”的对象,该对象具有两个属性:1)searchFor(来自文本输入)2)searchBy(来自下拉菜单),如下所示:

<div class="container">
<div class="row">
    <div class="col-sm-12 bevisible">
        <h3>Search for chemical entries</h3>
    </div>
</div>
<form>
    <div class="form-group row">
        <label for="searchFor" class="col-sm-2 col-form-label">Search For</label>
        <div class="col-sm-10">
            <input type="text" [(ngModel)]="chemical.searchFor"
                   name="searchbox"
                   class="form-control" id="searchFor"
                   placeholder="Search an Entry...">
        </div>
    </div>
    <div class="form-group row">
        <label for="searchType" class="col-sm-2 col-form-label">Search by</label>
        <div class="col-sm-10">
            <select class="custom-select" id="searchType" [(ngModel)]="chemical.searchBy" name="searchdropdown">
                <option selected>Search type</option>
                <option value="1">Reagent Barcode</option>
                <option value="2">Catalog#</option>
                <option value="3">Bottle Label</option>
                <option value="3">Location Barcode</option>
            </select>
        </div>
    </div>
    <div class="form-group row">
        <div class="offset-sm-2 col-sm-10">
            <button type="submit" (click)="getChemicalEntries(chemical)" class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>

After the chemical object is created, I want to pass it to getChemicalEntries(chemical) function defined on the component end. 创建化学对象之后,我要将其传递给组件端定义的getChemicalEntries(chemical)函数。

This is the associated component: 这是相关的组件:

import {Component, Input} from '@angular/core';
import {SearchService} from '../services/searchservice.component';
import {Chemical} from "../chemical";

@Component({
    selector: 'bioshoppe-search',
    providers:[SearchService],
    templateUrl: 'app/search/search.component.html',
    styleUrls: ['../../css/search.component.css', '../../css/style.css']
})

export class SearchComponent {
    @Input () chemical : Chemical;
    public chemicals;
    public error;

    constructor(private searchService: SearchService) {
    };

    // ngOnInit() {
    //     this.getChemicals();
    // }

    getChemicals() {
        this.searchService
            .getChemicalEntries()
            .subscribe(
                // the first argument is a function which runs on success
                data => {
                    this.chemicals = data
                },
                // the second argument is a function which runs on error
                err => {
                    console.error(err), this.error = true
                },
                // the third argument is a function which runs on completion
                () => console.log("done loading chemicals")
            );
    }
}

PS: I am new to Angular2. PS:我是Angular2的新手。 Have experience with Angular 1.4. 有使用Angular 1.4的经验。

This is actually a simple JavaScript error. 这实际上是一个简单的JavaScript错误。 You are trying to access searchFor property of chemical object from your template. 您正在尝试从模板访问chemical对象的searchFor属性。 But the chemical object is undefined when the template first try to access its property and that's why you are getting the error. 但是,当模板首次尝试访问其chemical属性时,该chemical对象是未定义的,这就是为什么会出现错误。 To solve this you just need to assign an empty or default object to 'chemical'. 为了解决这个问题,您只需要为“ chemical”分配一个空的或默认的对象。 For example: 例如:

@Input() chemical: Chemical = {
  searchFor: '',
  searchBy: ''
};

The bellow will also work fine but the above one is best practice for TypeScript. 波纹管也可以正常工作,但是以上是TypeScript的最佳实践。

@Input() chemical: any = {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM