在Ng2智能表中创建新数据

时间:2019-10-18 16:13:50

标签: javascript angular typescript ng2-smart-table

我在我的angular 2项目中使用了[ng2 smart table],我需要使用Http post方法发送一个API请求,但是当我单击按钮以确认数据时在控制台中遇到此错误时出现了问题:

  

core.js:7187错误TypeError:将圆形结构转换为JSON       ->从具有构造函数“ Subscriber”的对象开始       |属性'_subscriptions'->具有构造函数'Array'的对象       |索引0->具有构造函数'SubjectSubscription'的对象       ---属性“ _parent”封闭了圆圈       在JSON.stringify()       在HttpRequest.serializeBody(http.js:804)       在Observable._subscribe(http.js:1858)       在Observable._trySubscribe(Observable.js:42)       在Observable.subscribe(Observable.js:28)       在subscribeTo.js:20       在subscribeToResult(subscribeToResult.js:7)       在MergeMapSubscriber._innerSub(mergeMap.js:59)       在MergeMapSubscriber._tryNext(mergeMap.js:53)       在MergeMapSubscriber._next(mergeMap.js:36)

我制作了一种在 service.ts

中向API发布新数据的方法
import { Injectable } from '@angular/core';
import { Clients } from './clients.model';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable} from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class ClientsService {

  url="http://localhost:21063/api/clints"
  clients:Clients[];
  client:Clients;

  constructor(private http:HttpClient) { }
getAllClients(): Observable<Clients[]>{
    return this.http.get<Clients[]>(this.url);

}
addClient(client){
return this.http.post(this.url,client)

}

我的compontent.ts

import { Clients } from './../clients.model';
import { Component, OnInit } from '@angular/core';
import { IAngularMyDpOptions, IMyDateModel } from 'angular-mydatepicker';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ClientsService } from '../clients.service';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';



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

  // start main stores tbl
  settMain = {
    noDataMessage: 'عفوا لا توجد بيانات',

    actions: {
      columnTitle: 'إجراءات',
      position: 'right',
    },
    pager: {
      perPage: 5,
    },
    add: {
      addButtonContent: '  إضافة جديد ',
      createButtonContent: '',
      cancelButtonContent: '',
      confirmCreate: true,
    },
    edit: {
      editButtonContent: '',
      saveButtonContent: '',
      cancelButtonContent: '',
      confirmSave: true,

    },
    delete: {
      deleteButtonContent: '',
      confirmDelete: true,
    },

    columns: {
      id: {
        title: 'كود العميل',
        width: '80px',
      },
      name: {
        title: 'اسم العميل',
        width: '160px'
      },
      phone: {
        title: ' الهاتف'
      },
      address: {
        title: ' العنوان'
      },
      account: {
        title: 'الرصيد '
      },
      nots: {
        title: 'ملاحظات'
      }
    }
  };
  // end main stores tbl
  //start date option
  myOptions: IAngularMyDpOptions = {
    dateRange: false,
    dateFormat: 'yyyy/mm/dd',
    firstDayOfWeek: 'sa',
    sunHighlight: false,

    // other options... 
  };

  private myForm: FormGroup;
  constructor(private formBuilder: FormBuilder, private Service: ClientsService) { }


  ngOnInit() {

    this.Service.getAllClients().subscribe(data => this.Service.clients = data);

  onAddClient(event){
    this.Service.addClient(event).subscribe(
      res=>{
        console.log(res);

        event.confirm.resolve(event.clients);
      },

    )
  } 

和我的模板

 <div class="mainTbl">

            <ng2-smart-table 
            [settings]="settMain" 
            [source]="this.Service.clients"
            (createConfirm)="onAddClient($event)"
            (editConfirm)="onEditConfirm($event)"
            (deleteConfirm)="onDeleteConfirm($event)"
            ></ng2-smart-table>

        </div>
    </div> <!-- end main stores tbl-->

所以问题是我的错在哪里?在ng2智能表中发送数据的最佳方法是什么?

0 个答案:

没有答案