Angular 2在更改模型后不更新DOM
我在angular2上遇到了一些我无法理解的问题。问题是,在HTTP请求正确完成后,视图不会更新 我正在重新分配变量(不更新引用中的对象),并且还测试了一些基于布尔值的*ngIf属性的额外div。Dom不会更新,divs和{{{object interpolation} 这是一种观点:Angular 2在更改模型后不更新DOM,dom,angular,updates,Dom,Angular,Updates,我在angular2上遇到了一些我无法理解的问题。问题是,在HTTP请求正确完成后,视图不会更新 我正在重新分配变量(不更新引用中的对象),并且还测试了一些基于布尔值的*ngIf属性的额外div。Dom不会更新,divs和{{{object interpolation} 这是一种观点: <div *ngIf="isLoading">Loading data...</div> <div *ngIf="!isLoading">{{ perfil | json }}
<div *ngIf="isLoading">Loading data...</div>
<div *ngIf="!isLoading">{{ perfil | json }}</div>
Debug: {{ perfil | json }}
<a (click)="test()" >Click test</a>
正在加载数据。。。
{{perfil | json}
调试:{perfil | json}
点击测试
这是组件代码:
export class GitTestComponent implements OnInit{
perfil: GithubProfile = <GithubProfile>{}; //Also tested just "perfil: GithubProfile;"
isLoading: boolean = true;
constructor(private _githubService: GithubService){
}
ngOnInit(){
this._githubService.getProfile('jaimehrubiks')
.timeout(5000)
.subscribe(
(profile) => this.perfil = profile,
(error) => console.log(error),
() => this.isLoading = false
)
}
test(){
}
}
导出类GitTestComponent实现OnInit{
perfil:GithubProfile={};//也只测试了“perfil:GithubProfile;”
isLoading:布尔值=true;
构造函数(私有的_githubService:githubService){
}
恩戈尼尼特(){
这个._githubService.getProfile('jaimehrubiks'))
.超时(5000)
.订阅(
(profile)=>this.perfil=profile,
(错误)=>console.log(错误),
()=>this.isLoading=false
)
}
测试(){
}
}
值得注意的是,如果我单击锚点或任何分配了事件的元素,DOM实际上会更新
以下是出现问题时的维修代码:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http'
import {Observable} from 'rxjs/Observable' //Also tested rxjs/Rx
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/operator/map';
import {GithubUser, GithubProfile, GithubFollowers} from './github.interface'
@Injectable()
export class GithubService{
usersUrl = "https://api.github.com/users/";
constructor(private _http: Http){
}
getUser(user: string) : Observable<GithubUser>{
return this._http.get(this.usersUrl + user)
.map(res => res.json());
}
getFollowers(user: string) : Observable<GithubFollowers>{
return this._http.get(this.usersUrl + user + '/followers')
.map(res => res.json());
}
getProfile(user: string) : Observable<GithubProfile>{
return Observable.forkJoin(
this.getUser(user),
this.getFollowers(user))
.map( joined => <GithubProfile> new Object( {user: joined[0], followers: joined[1]} ) )
}
}
从'@angular/core'导入{Injectable};
从“@angular/Http”导入{Http}
从'rxjs/Observable'导入{Observable}//也测试了rxjs/Rx
导入“rxjs/add/observable/forkJoin”;
导入'rxjs/add/operator/map';
从“./github.interface”导入{GithubUser,GithubProfile,GithubFollowers}
@可注射()
导出类服务{
usersUrl=”https://api.github.com/users/";
构造函数(私有的http:http){
}
getUser(用户:字符串):可观察{
返回此。\u http.get(this.usersUrl+user)
.map(res=>res.json());
}
getFollowers(用户:字符串):可观察{
返回此。\u http.get(this.usersUrl+user+'/followers')
.map(res=>res.json());
}
getProfile(用户:字符串):可观察{
返回可观察的forkJoin(
这个.getUser(用户),
此文件名为.getFollowers(用户))
.map(joined=>new对象({user:joined[0],followers:joined[1]}))
}
}
如果改变是由不在角度区域的动作推动的,则通常会发生这种情况。我不确定您提供的代码中有什么会触发区域外模型更新,但可能是相同的。好的,谢谢。我修复了它:this.\u zone.run(()=>{this.isLoading=false})但是我不明白为什么它一开始就跑出了这个区域。我想知道如何防止服务超出ng区域。