Действие кнопки вызова службы веб - PullRequest
0 голосов
/ 08 марта 2019

Я новичок в угловых и хочу добавить, чтобы добавить кнопку действие, которое вызывает веб-сервис, который отправляет себя на сервер узла, чтобы добавить пользователя.я работаю над шаблоном (nebular-ngx). введите описание ссылки здесь

это код моей кнопки добавления с angular4

 add: {
  addButtonContent: '<i class="nb-plus"></i>',
  createButtonContent: '<i class="nb-checkmark"></i>',
  cancelButtonContent: '<i class="nb-close"></i>',
},

я хочу добавить действие, которое вызывает веб-службу и отправляет ее на узелсервер

это мой дырочный код

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();
 }
 }
 }

1 Ответ

0 голосов
/ 08 марта 2019

Вы можете использовать событие нажатия кнопки, чтобы выполнить сервисный вызов из компонента:

<button class="btn btn-primary" (click)="makeServiceCall($event)">Add</button>

В компоненте:

makeServiceCall(e) {
       this.myAngularService.makeHttpRequest().subscribe((response: Response) => {
            // my response here
        }, (error: any) => {
            console.log(error);
        });

}

Внутри myAngularService, используйте Angular's 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);
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...