Asp.net mvc 角度2字符串在第一次迭代时保持未定义
我试图通过http.get从服务器获取一个对象,它可以工作,但是在第一次迭代中字符串仍然没有定义。 整数没有问题,问题只在于字符串Asp.net mvc 角度2字符串在第一次迭代时保持未定义,asp.net-mvc,typescript,angular,Asp.net Mvc,Typescript,Angular,我试图通过http.get从服务器获取一个对象,它可以工作,但是在第一次迭代中字符串仍然没有定义。 整数没有问题,问题只在于字符串 this.http.get("/Home/getRoom", { headers: headers }).map(res => res.json()).subscribe(q => { this.timecount = q.timeperquestion; this.player1id = JSON.stringify(q.pl
this.http.get("/Home/getRoom", { headers: headers }).map(res => res.json()).subscribe(q => {
this.timecount = q.timeperquestion; this.player1id = JSON.stringify(q.player1id); this.player2id = q.player2id; }, err => console.error(err),
() => console.log('Complete'));
在函数的第一次迭代之后,将显示字符串
编辑:
这是一节课:
class AppComponent implements AfterViewInit {
public answered = false;
public title = "loading question...";
public options = [];
public correctAnswer = false;
public working = false;
public timecount=15;
public timer;
public roomID;
public player1id;
public player2id;
constructor( @Inject(Http) private http: Http, private Ref: changeDetectorRef) {
}
nextQuestion() {
this.working = true;
this.answered = false;
this.title = "loading question...";
this.options = [];
var headers = new Headers();
headers.append('If-Modified-Since', 'Mon, 27 Mar 1972 00:00:00 GMT');
this.http.get("/Home/getRoom", { headers: headers }).map(res => res.json()).subscribe(q => {
this.timecount = q.timeperquestion;
this.player1id = JSON.stringify(q.player1id);
this.Ref.detectChanges();
},
err => console.error(err),
() => console.log('Complete'));
编辑2:
好的,字符串似乎一切正常,当我尝试用{{player1id}显示它时,即使在第一次迭代中,它也会显示正确的值。
“问题”只存在于我用来检查字符串的console.log(player1id)中,它只在一次迭代后才起作用。
谢谢你的帮助 您可以使用
ChangeDetectorRef
类强制运行更改检测。注入它并调用它的detectChanges
方法:
constructor(private changeDetectorRef:changeDetectorRef) {
}
someMethod() {
this.http.get("/Home/getRoom", { headers: headers }).map(res => res.json()).subscribe(q => {
this.timecount = q.timeperquestion;
this.player1id = JSON.stringify(q.player1id);
this.player2id = q.player2id;
this.changeDetectorRef.detectChanges();
},
err => console.error(err),
() => console.log('Complete'));
}
属性未定义的原因是它们实际上未定义。只需将它们设置为空字符串
=”
,或将它们声明为:字符串,如下所示:
class AppComponent implements AfterViewInit {
public player1id: string;
public player2id = ""; // I usually prefer this as it is assigned a value
// Omitted for brevity...
}
在TypeScript中,您应该声明要使用的类型,或者将其赋值为可以推断的类型。否则你就有点错过了打字脚本的所有荣耀强制执行检测更改
不是一个糟糕的做法吗?是的,同意!如果没有这个,它应该可以工作,但在这种情况下,出于某些原因,它似乎是必需的…我得到了TypeError:无法读取未定义(…)的属性'detectChanges',当您注入到组件中时,我看到了一个输入错误。它应该是:constructor(@Inject(Http)private-Http:Http,private-Ref:ChangeDetectorRef){
而不是constructor(@Inject(Http)private-Http:Http,private-Ref:ChangeDetectorRef){
(ChangeDetectorRef
)。当我尝试构造函数(@Inject(Http)时,它应该是相同的private http:http,@Inject(ChangeDetectorRef)private Ref:ChangeDetectorRef(我不确定是否是这样注入多个项)我遇到异常:无法解析AppComponent的所有参数(@Inject(http),@Inject(未定义))。请确保它们都具有有效的类型或批注。标题定义为什么,您可以共享调用它的类吗?没有足够的信息提供相关的答案。好的,很抱歉,谢谢!我已经尝试过了,但问题是第一次迭代时值仍然为空。您可以放置t吗在plunker中,必须有其他事情可以合理解释。不需要使用detectChanges
。