0
This is my html:
<div class="container">
<form [formGroup]="form" (ngSubmit)="postCreateTypeFields()" style="width:400px; margin: 0 auto;">
<h1>Types Fields</h1>
<div class="required-field-block">
<input formControlName="name" type="text" placeholder="Nome" class="form-control">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<div class="required-field-block">
<input formControlName="version" type="text" placeholder="Versão" class="form-control">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<button type="submit" class="btn btn-primary" >Criar</button>
</form>
</div>
Component
import { Component, OnInit, ModuleWithProviders } from '@angular/core';
import { FormBuilder, FormGroup, NgControl } from '@angular/forms';
import { CreateTypeFieldsService } from '../create-type-fields.service';
@Component({
selector: 'app-create-type-fields',
templateUrl: './create-type-fields.component.html',
styleUrls: ['./create-type-fields.component.css'],
providers: []
})
export class CreateTypeFieldsComponent implements OnInit {
private _createTypesFields: Model.CreateTypesFields;
private form: FormGroup;
constructor(private _createTypeService: CreateTypeFieldsService, private builder: FormBuilder) {
this.form = this.builder.group({
name: '',
version: ''
})
}
ngOnInit() {
this._createTypeService.postCreateTypeFields(this._createTypesFields)
.subscribe( success => {
if(success.Result){
//anything here
}
},
error =>{
}
);
}
}
Service
import { Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Configuration } from './app.constants';
import {RequestOptions, Request, RequestMethod} from '@angular/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable({
providedIn: 'root'
})
export class CreateTypeFieldsService {
private actionUrl: string;
private url: 'http://localhost:56758/api';
constructor(private http: HttpClient, private _configuration: Configuration) {
this.actionUrl = _configuration.ServerWithApiUrl + 'TypesFields/';
}
public postCreateTypeFields(itemName: Model.CreateTypesFields): Observable<any> {
const toAdd = JSON.stringify({ ItemName: itemName });
return this.http.post(this.actionUrl, toAdd, httpOptions);
/
}
}
when I try to consume the API, it makes that mistake
Typeerror: _co.postCreateTypeFields is not a Function
How do I fix it?
Celso, in my ngOnInit of the Component I have this:
ngOnInit() {
 this._createTypeService.postCreateTypeFields(this._createTypesFields)
 .subscribe( success => {
 if(success.Result){
 //anything here
 }
 }
Where would I put this class? In the same Komponent? In ngOnInit, changing to the above example?– pnet
As I suggested, in the function should solve. I did not understand very well what the purpose of that snippet of code is. But note that the code within the
ngOnInit
will be called after loading the component only once. When we move to the function it is called every click on it.– Celso Marigo Jr
Hello, really it was all wrong. I followed your guidance, removed the code inside Init and solved. Now generates the mongodb GUID, but does not save the Name field from the textbox, but this is another post. I will close and mark your reply.
– pnet