Angular 如何以角形和组件ts发布数据
我怎样才能用这个表格发布五月的数据? 我真的很难做到这一点,我需要你在这方面的经验 我在下面添加了我的service.ts。我真的是一个新手,我希望任何人都能给我的见解 提前谢谢大家 应用程序组件.tsAngular 如何以角形和组件ts发布数据,angular,typescript,angularjs-ng-form,Angular,Typescript,Angularjs Ng Form,我怎样才能用这个表格发布五月的数据? 我真的很难做到这一点,我需要你在这方面的经验 我在下面添加了我的service.ts。我真的是一个新手,我希望任何人都能给我的见解 提前谢谢大家 应用程序组件.ts import { Component } from '@angular/core'; import { NgForm } from '@angular/forms'; import { TextService } from './text.service' import { Text } fro
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { TextService } from './text.service'
import { Text } from './text';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [TextService]
})
export class AppComponent {
constructor(public textService:TextService){}
ngOnInit(){
this.getTexts()
}
getTexts(){
this.textService.getTexts()
.subscribe(( res ) => {
this.textService.texts = res as Text[];
})
}
postText(formValue){
this.textService.postText(formValue).subscribe( res => {
this.getTexts();
this.textService.selectedText = new Text()
});
}
}
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { TextService } from './text.service'
import { Text } from './text';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [TextService]
})
export class AppComponent {
constructor(public textService:TextService){}
ngOnInit(){
this.getTexts()
}
getTexts(){
this.textService.getTexts()
.subscribe(( res ) => {
this.textService.texts = res as Text[];
})
}
postText(formValue){
this.textService.postText(formValue).subscribe( res => {
this.getTexts();
this.textService.selectedText = new Text()
});
}
}
app.service.ts
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { TextService } from './text.service'
import { Text } from './text';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [TextService]
})
export class AppComponent {
constructor(public textService:TextService){}
ngOnInit(){
this.getTexts()
}
getTexts(){
this.textService.getTexts()
.subscribe(( res ) => {
this.textService.texts = res as Text[];
})
}
postText(formValue){
this.textService.postText(formValue).subscribe( res => {
this.getTexts();
this.textService.selectedText = new Text()
});
}
}
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { TextService } from './text.service'
import { Text } from './text';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [TextService]
})
export class AppComponent {
constructor(public textService:TextService){}
ngOnInit(){
this.getTexts()
}
getTexts(){
this.textService.getTexts()
.subscribe(( res ) => {
this.textService.texts = res as Text[];
})
}
postText(formValue){
this.textService.postText(formValue).subscribe( res => {
this.getTexts();
this.textService.selectedText = new Text()
});
}
}
app.components.html
<div *ngFor="let text of textService.texts">
<h1> {{ text.some_text }} </h1>
</div>
<form #textForm="ngForm" (ngSubmit)="postText(textForm.value)">
<label for="some_text">First name:</label><br>
<input type="text" id="some_text" name="some_text" #name="ngModel" [(ngModel)]="textService.selectedText.some_text"><br>
<input type="submit" value="Submit">
</form>
{{text.some_text}
名字:
您的打字脚本一开始格式不好
postText(textForm=NgForm){
.subscribe((res) => {
this.getTexts()
this.textService.selectedText = new Text()
})
您没有订阅此方法内第一行的任何内容。相反,您应该使用您的文本服务(我假设您使用http)来发布您从html传递的表单值。如下所示:
postText(formValue){
this.textService.post(formValue).subscribe( res => {
this.getTexts();
this.textService.selectedText = new Text()
});
}
.post“类型“TextService”上不存在属性“post”。ts(2339)“我更新了问题中的脚本。根据您的回答,可能html格式的表单是错误的。你能检查一下吗?看起来不错。您需要在您的服务上实现post(您使用了postText),在该服务中,您使用httpclient发布表单并返回可观察内容。您好,我添加了我的app.service.ts。您能检查一下吗?我建议您在此处查看有关向服务器发布数据的文档: