I would like to call a php file from Angular passing the username and password parameters.
import { Component, OnInit } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Component({
selector: 'app-api-login',
templateUrl: './api-login.component.html',
styleUrls: ['../app.component.css']
})
export class ApiLoginComponent implements OnInit {
usuario:String;
clave:String;
register:Boolean;
constructor() {
this.usuario="PrecaNuevo"
this.clave="PrecaNuevo";
this.register=false;
}
ngOnInit() {
var conAjax = $http.post("pgular/php/login.php", {usuario: this.usuario, clave: this.clave});
conAjax.success(function(respuesta){
console.log(respuesta);
});
}
}
I get the error:ERROR ReferenceError: $http is not defined
I have started today with Angular, sure some things are missing.
I recommend you take a look at the angular tutorial .
So, quickly,
$http
it doesn't exist in your component. Angular works on dependency injection, that is, to use angular's http client you must inject it into the constructor:To make the call would be:
And for this to work you must import the HttpClientModule module in your AppModule.
But the most important thing in my opinion is that you do the tutorial that I linked above.