gpt4 book ai didi

javascript - 如何从另一个组件 angular 4 提交表单?

转载 作者:行者123 更新时间:2023-11-28 00:40:25 25 4
gpt4 key购买 nike

我被困在某事上,我需要你的帮助。

我想提交一个在另一个组件中的表单,而提交按钮在另一个组件中。

我怎样才能在 Angular 4 中实现它?

这是我的代码的 html。

它的表单的html

    <form class="form form-validate" ngNativeValidate 
(submit)="editInformationFunction($event)"
#editForm="ngForm">
<div class="row px-3 pb-3">

<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">
<!---------------Panel1 ------------------->
<div class="panel panel-primary radius">
<div class="panel-heading border-bottom p-2">
<h3 class="panel-title m-0"><i class="fa fa-user green pl-3">
</i> <span
class="ml-2 font-weight-bold">PERSONAL INFORMATION</span> .
</h3>
<span class="pull-right "><i class="glyphicon glyphicon-
chevron-down"> </i></span>
</div>
<div class="panel-body">

<div class="row">

<!--input fields-->

<div class="col-xs-12 col-sm-12 col-md-12 col-md-6 col-lg-6
">
<div class="form-group mt-4 ml-4">
<label for="name"><b>NAME<span class="text-danger ml-2">*
</span> </b></label>
<input id="name" type="text" class="form-control"
[(ngModel)]="editInformationModel.name"
[ngModelOptions]="{standalone: true}" required>
</div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12 col-md-6 col-lg-6
">
<div class="form-group mt-4 mr-4">
<label for="info"><b>BIRTH DATE
</b></label>
<input id="info" type="text" class="form-control"
[(ngModel)]="editInformationModel.birthdate ?
editInformationModel.birthdate : N/A "
[ngModelOptions]="{standalone: true}" required>
</div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="form-group form-check-inline mt-1 ml-4">
<b>GENDER<span class="text-danger ml-2">*</span></b>

<div class="form-check-inline d-flex mt-1">

<label class="customradio"><span class="radiotextsty">
<b>Male</b></span>
<input type="radio" checked="checked" name="radio"
[(ngModel)]="editInformationModel.sex"
[ngModelOptions]="{standalone: true}"
value="m">
<span class="checkmark"> </span>
</label>
<label class="customradio ml-2"><span
class="radiotextsty"><b>Female</b></span>
<input type="radio" name="radio"
[(ngModel)]="editInformationModel.sex"
[ngModelOptions]="{standalone: true}"
value="f">
<span class="checkmark"> </span>
</label>

</div>

</div>
</div>
</form>

它的形式.ts文件

@Component({
selector: 'app-edit-information',
styleUrls: ['./edit-information.component.css'],
templateUrl: './edit-information.component.html'
})
export class EditInformationComponent implements OnInit {
editInformationFunction(e) {
console.log(this.editInformationModel);
this.editInformationService.editMemberInformation
(this.editInformationModel).subscribe(
response => {
console.log(response);
this.spinner.hide();
if (response ['error'] == false) {
console.log(response);
this.toastr.success(response['data'],
this.editInformationData.toastTitle.success);
} else {
this.spinner.hide();
this.toastr.error(response['message'],
this.editInformationData.toastTitle.error);
}
},
err => {
this.spinner.hide();
this.toastr.error(err.error['message'],
this.editInformationData.serverError);
}
)
}
}

这是我要提交表单的侧边栏 html....

  <div class="d-flex" [hidden]="saveInformation">
<button type="submit" class="btn btn-success btn-sm py-2 border-0"><i
class="fa fa-save"> </i>
Save
Information
</button>
<button class="btn btn-default btn-sm text-white ml-2 py-2"
routerLink="/household-information">
Cancel
</button>
</div>

侧边栏的.ts文件

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

最佳答案

如果您的组件是 time 更简单的方法是使用 @Input 并在引用变量中传递您的组件。例如你的 app.component 就像

<hello #hello name="{{ name }}"></hello>
<nav-bar [component]="hello"></nav-bar>

//你的导航栏就像

@Component({
selector: 'nav-bar',
template: `<button (click)="click()">click me</button>`,
styles: [`h1 { font-family: Lato; }`]
})
export class NavBarComponent {
@Input()component:any
click()
{
console.log(this.component.variable)
}
}

“经典”是使用服务。您的服务可以像

@Injectable()

export class DataService {

private mySubjectSource = new Subject<any>();
myObservable=this.mySubjectSource.asObservable();

constructor(){}

sendValue(value:any)
{
this.mySubjectSource.next(value);
}
}

然后你的组件使用这个服务:

发出值的组件

export class NavBarComponent  {
constructor(private service:DataService){}
click2()
{
this.service.sendValue("save data")
}
}

接收事件的组件

export class HelloComponent implements OnInit {
constructor(private service:DataService){ }
ngOnInit()
{
this.service.myObservable.subscribe(res=>{
console.log(res);
})
}
}

this stackblitz我把这两种方式放在一起

选择要提交的位置:在导航中获取表单的值,或者在从导航发出事件时在表单中-

关于javascript - 如何从另一个组件 angular 4 提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53801079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com