-1
我得到一個eroor爲this.loginservicemodel.loginHttpCall不是函數。我將角2結構分爲三部分 - 1)組件2)查看模型和3)服務。(XXX)不是函數angular 2 asp.net核心
組件將與ViewModels和服務進行通信。服務可以訪問viewModel。
我返回服務文件中的函數如下
loginservice.ts
import { Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { LoginViewModel } from "../ViewModel/Login.ViewModel";
@Injectable()
export class LoginService {
private heroesUrl = 'app/heroes'; // URL to web API
private loginModel = LoginViewModel;
constructor(private http: Http) { }
public loginHttpCall() {
console.log(this.loginModel);
let headers = new Headers({ 'Content-Type': 'application/json' });
this.http.post('/contact/send', JSON.stringify(this.loginModel), { headers: headers })
.subscribe();
}
}
Login.ViewModel.ts
export class LoginViewModel {
userName: string;
password: string;
}
login.Components.ts
import { Component} from '@angular/core';
import { LoginViewModel } from "../ViewModel/Login.ViewModel";
import { LoginService} from "../Service/Login.Service";
@Component({
selector: 'login-app',
templateUrl: 'Home/partialLogin',
providers: [LoginViewModel, LoginService]
})
export class LoginComponent {
private model = LoginViewModel;
private loginservicemodel: LoginService;
constructor() {
this.model.userName = '[email protected]';
this.model.password = "test anand";
}
login(event) {
this.loginservicemodel.loginHttpCall();
event.preventDefault();
}
}
登錄(事件)函數被調用時,按一下按鈕。
的方法調用this.loginservicemodel.loginHttpCall();
功能,但我得到一個錯誤
TypeError: Cannot read property 'loginHttpCall' of undefined
Module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { Http } from '@angular/http';
import { LoginComponent } from "./Components/login.Component";
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [LoginComponent],
bootstrap: [LoginComponent]
})
export class AppModule { }
我知道它沒有得到方法,但我在哪裏做的錯誤我不知道。
任何人都可以幫助我這個。
我得到loginservice.ts中的另一個錯誤爲「沒有提供程序的Http!」。你可以幫我解決這個問題 –
@SanJaisy http://stackoverflow.com/questions/33721276/angular-2-no-provider-for-http – echonax
該解決方案不適合我.....因爲我我在service.ts文件中使用http –