简体   繁体   中英

Passing HTML variables to Angular Component

I am trying to pass the items written in a text-box field on my webpage to variables in the component file. These variables will then be used in the service file that has a function link to the POST request I made on the python/SQLAlchemy side. I am using Flask with python/SQLAlchemy on the backend, and Angular CLI 7.0.5. Here's what I have:

 <div class = "container"> <input id="InsertItemName" [(ngModel)]="InsertItemName"/> <input id="InsertItemManu" [(ngModel)]="InsertItemManu"/> <input id="InsertItemType" [(ngModel)]="InsertItemType"/> <button (click)="sendValues()">Send</button> </div> 

modify-page.component.html:

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Data } from '@angular/router';
import { HttpResponse } from '@angular/common/http';
import { SelectItem } from 'primeng/components/common/selectitem';
import { MenuItem } from 'primeng/api';
import { ModifyService, ItemsData } from '../modify.service'
import { PARAMETERS } from '@angular/core/src/util/decorators';



@Component({
  selector: 'app-modify-page',
  templateUrl: './modify-page.component.html',
  styleUrls: ['./modify-page.component.css']
})
export class ModifyPageComponent implements OnInit {

  InsertItemName: string;
  InsertItemManu: string;
  InsertItemType: string;

  insertItems: ItemsData;


  constructor(
    private modifyService: ModifyService,
    private route: ActivatedRoute
  ) {
   }

  ngOnInit() {
    this.insertItems.name = this.InsertItemName;
    this.insertItems.manufacture = this.InsertItemManu;
    this.insertItems.type = this.InsertItemType;

  }


  sendValues(): void {
    console.log(this.InsertItemName, this.InsertItemManu, this.InsertItemType)

    this.modifyService.postInputItems(this.insertItems)


}


}

modify.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

export interface ItemsData {
  name: string;
  manufacture: string;
  type: string;
}

@Injectable({
  providedIn: 'root'
})
export class ModifyService {

  constructor(
    public http: HttpClient
  ) { }


postInputItems(data: ItemsData){
  return this.http.post('/modify/insertItems', data)
}

}

And if you need it, this is the database.py:

def insert_dbItems(a, b, c):
        with engine.connect() as con:
                ins = Items.insert().values(name = a, manufacture = b, type = c)
                con.execute(ins)
        return "Successfully inserted data into Items table"

and the init .py:

@app.route('/api/modify/insertItems', methods=["POST"])
def insert_Itemsdb():
        body = json.loads(request.data)
        a = body['name']
        b = body['manufacture']
        c = body['type']
        return jsonify(database.insert_dbItems(a, b, c))

The database and init files work, I can use the Postman app and correctly insert the variables into my database from it. My problem is that when I run all the code above, I get left with this:

enter image description here

In conclusion: everything here is for me to take an input from a user and insert it into my database. Any help would be fantastic, thank you!

I think you forgot to initialize insertItems at the top of your ModifyPageComponent.

  insertItems: ItemsData = {
    name: null;
    manufacture: null;
    type: null;
  }

Hope that will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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