【发布时间】:2018-11-18 00:31:28
【问题描述】:
我正在尝试将我网页上的文本框字段中写入的项目传递给组件文件中的变量。然后,这些变量将在服务文件中使用,该文件具有指向我在 python/SQLAlchemy 端发出的 POST 请求的函数链接。我在后端使用 Flask 和 python/SQLAlchemy,以及 Angular CLI 7.0.5。这是我所拥有的:
<div class = "container">
<input id="InsertItemName" [(ngModel)]="InsertItemName"/>
<input id="InsertItemManu" [(ngModel)]="InsertItemManu"/>
<input id="InsertItemType" [(ngModel)]="InsertItemType"/>
<button (click)="sendValues()">Send</button>
</div>
修改-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)
}
}
修改.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)
}
}
如果你需要,这就是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"
和 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))
数据库和初始化文件工作正常,我可以使用 Postman 应用程序并从中正确地将变量插入到我的数据库中。我的问题是,当我运行上面的所有代码时,我得到了这个:
总结:这里的一切都是为了让我从用户那里获取输入并将其插入到我的数据库中。任何帮助都会很棒,谢谢!
【问题讨论】:
标签: javascript database angular typescript flask-sqlalchemy