点击事件不在angular2中工作

时间:2016-04-07 13:56:44

标签: angular

我是angular2中的新手。我在调用click事件时遇到问题。它给了我错误。这是我的代码:

<form>
    <input ngControl="email" type="email" placeholder="Your email">
    <input ngControl="password" type="password" placeholder="Your password">
  <button  (click)="postData()">Log in</button>
</form>

这是我的组件:

import {Component} from 'angular2/core';
import {NgForm}    from 'angular2/common';
import { Router } from 'angular2/router';
@Component({
  selector: 'my-form',
  templateUrl: 'app/form.component.html'
})
export class FormComponent {
    postData(){}
};

它给了我错误:

EXCEPTION: Error during evaluation of "click"

1 个答案:

答案 0 :(得分:1)

我猜...你可能忘了将http服务注入组件的构造函数中:

import {HTTP_PROVIDERS} from 'angular2/http';
@Component({
   providers: [HTTP_PROVIDERS],
   ...
})
export class FormComponent {
    constructor(private _http:Http) {}
    postData() {
       this._http.post(...);
    }
}