[英]Action button invoke service web
I am beginner in angular and i want to add to add button an action that invokes a web service that sends itself to a node server to add a user.我是 angular 的初学者,我想向添加按钮添加一个操作,该操作调用 Web 服务,该服务将自身发送到节点服务器以添加用户。 i work on a template (nebular-ngx) .我在一个模板 (nebular-ngx) 上工作。 enter link description here在此处输入链接描述
this the code of my add button with angular4这是我的带有 angular4 的添加按钮的代码
add: {
addButtonContent: '<i class="nb-plus"></i>',
createButtonContent: '<i class="nb-checkmark"></i>',
cancelButtonContent: '<i class="nb-close"></i>',
},
i wonna add an action who invoke a service web and send it to a node server我想添加一个调用服务网络并将其发送到节点服务器的操作
this is my hole code这是我的漏洞代码
import { Component } from '@angular/core';
import { LocalDataSource } from 'ng2-smart-table';
import { SmartTableData } from '../../../@core/data/smart-table';
@Component({
selector: 'ngx-smart-table',
templateUrl: './smart-table.component.html',
styles: [`
nb-card {
transform: translate3d(0, 0, 0);
}
`],
})
export class SmartTableComponent {
settings = {
add: {
addButtonContent: '<i class="nb-plus"></i>',
createButtonContent: '<i class="nb-checkmark"></i>',
cancelButtonContent: '<i class="nb-close"></i>',
},
edit: {
editButtonContent: '<i class="nb-edit"></i>',
saveButtonContent: '<i class="nb-checkmark"></i>',
cancelButtonContent: '<i class="nb-close"></i>',
},
delete: {
deleteButtonContent: '<i class="nb-trash"></i>',
confirmDelete: true,
},
columns: {
id: {
title: 'ID',
type: 'number',
},
firstName: {
title: ' Name',
type: 'string',
},
email: {
title: 'E-mail',
type: 'string',
},
password: {
title: 'password',
type: 'password',
},
},
};
source: LocalDataSource = new LocalDataSource();
constructor(private service: SmartTableData) {
const data = this.service.getData();
this.source.load(data);
}
onDeleteConfirm(event): void {
if (window.confirm('Are you sure you want to delete?')) {
event.confirm.resolve();
} else {
event.confirm.reject();
}
}
}
You can use your button click event to make the service call from component:您可以使用按钮单击事件从组件进行服务调用:
<button class="btn btn-primary" (click)="makeServiceCall($event)">Add</button>
In component:在组件中:
makeServiceCall(e) {
this.myAngularService.makeHttpRequest().subscribe((response: Response) => {
// my response here
}, (error: any) => {
console.log(error);
});
}
Inside myAngularService
, make use of angular's HttpClient to make the service call:在myAngularService
,利用 angular 的HttpClient进行服务调用:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class myAngularService{
constructor(private http: HttpClient) { }
makeHttpRequest() {
return this.http.get<any>('my_service_url);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.