gpt4 book ai didi

http - Angular2 http 订阅组件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:33 25 4
gpt4 key购买 nike

一个angular2应用,尝试注册一个邮箱。

import {Component, Directive, provide, Host} from '@angular/core';
import {NG_VALIDATORS, NgForm} from '@angular/forms';
import {ChangeDetectorRef, ChangeDetectionStrategy} from '@angular/core';
import {ApiService} from '../../services/api.service';

import {actions} from '../../common/actions';
import {EmailValidator} from '../../directives/email-validater.directive';
import * as _ from 'lodash';
import * as Rx from 'rxjs';

@Component({
selector: 'register-step1',
directives: [EmailValidator],
styleUrls: ['app/components/register-step1/register.step1.css'],
templateUrl: 'app/components/register-step1/register.step1.html'
})
export class RegisterStep1 {

email: string;
userType: number;
errorMessage: string;
successMessage: string;

constructor(private _api: ApiService, private ref: ChangeDetectorRef) {
this.successMessage = 'success';
this.errorMessage = 'error';

}

submit() {

var params = {
email: this.email,
type: +this.userType
};
params = {
email: '1@qq.com',
type: 3
};

this._api.query(actions.register_email, params).subscribe({
next: function(data) {
if(data.status) {
console.log("success register");
this.successMessage = "ok ,success";
console.log(this.errorMessage, this.successMessage);
}else{
this.errorMessage = data.message;
console.warn(data.message)
}
},
error: err => console.log(err),
complete: () => console.log('done')
});
}
}

我的 ApiService 很简单:

import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

import {AjaxCreationMethod, AjaxObservable} from 'rxjs/observable/dom/AjaxObservable';

import {logError} from '../services/log.service';
import {AuthHttp, AuthConfig, AUTH_PROVIDERS} from 'angular2-jwt';

@Injectable()
export class ApiService {
_jwt_token:string;

constructor(private http:Http) {

}


toParams(paramObj) {
let arr = [];
for(var key in paramObj) {
arr.push(key + '=' + paramObj[key]);
}
return arr.join('&')
}

query(url:string, paramObj:any) {
let headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'});
let options = new RequestOptions({headers: headers});

return this.http.post(url, this.toParams(paramObj), options).map(res=>res.json())
}

}

这是我的 html :

<form #f="ngForm">
usertype<input type="text" name="userType" [(ngModel)]="userType"><br/>
<input type="text" name="email" ngControl="email" email-input required [(ngModel)]="email">
<button [disabled]="!f.form.valid" (click)="submit(f.email, f.userType)">add</button>

</form>
{{f.form.errors}}
<span *ngIf="errorMessage">error message: {{errorMessage}}</span>
<span *ngIf="successMessage">success message: {{successMessage}}</span>

我可以成功地将 api 发送到服务器并收到响应,我订阅了一个观察者到 http 响应,它是一个 Observable 对象,在下一个函数中,我 console.log() 我的 successMessage,但我得到“undefined”,当我更改 successMessage 时,我的 html 没有变化。

好像我已经失去了我的组件的范围,那么我不能使用这个关键字

最佳答案

那是因为你在 TypeScript 中使用了 function 关键字。永远不要这样做。始终使用箭头符号 () => {}

您应该将next 函数更改为:

next: (data) => {
if(data.status) {
console.log("success register");
this.successMessage = "ok ,success";
console.log(this.errorMessage, this.successMessage);
}else{
this.errorMessage = data.message;
console.warn(data.message)
}

关于http - Angular2 http 订阅组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com