angular2*ngFor和*ngIf不更新
我在使用一个非常简单和常用的代码时遇到了问题。我在很多不同的项目中使用了它,但这次它不起作用,我疯狂地想知道为什么 这是一个简单的*ngFor数组循环。此数组是从http请求中检索的,该请求使用用户可以使用的菜单语音对其进行popole。 如果用户未登录,则此数组仅在登录操作后初始化为[]和POPOLE。我确信数组在控制台上的日志记录是正确的 我尝试了不同的选择,但没有人成功。 我在*ngIf上遇到了同样的问题,它使用相同的数组(array.length)来选择是否隐藏菜单 菜单angular2*ngFor和*ngIf不更新,angular,loops,Angular,Loops,我在使用一个非常简单和常用的代码时遇到了问题。我在很多不同的项目中使用了它,但这次它不起作用,我疯狂地想知道为什么 这是一个简单的*ngFor数组循环。此数组是从http请求中检索的,该请求使用用户可以使用的菜单语音对其进行popole。 如果用户未登录,则此数组仅在登录操作后初始化为[]和POPOLE。我确信数组在控制台上的日志记录是正确的 我尝试了不同的选择,但没有人成功。 我在*ngIf上遇到了同样的问题,它使用相同的数组(array.length)来选择是否隐藏菜单 菜单 menuCom
menuComponent: Array<any> = [];
open = false;
actualPlace = [];
controlKey = [];
constructor(
private router: Router,
public GFService: GeneralFunctionService,
public loginService: LoginService,
) {
}
ngOnInit() {
this.GFService.countThread('P');
this.GFService.getMenu().subscribe(
(res: any) => {
this.GFService.countThread('M');
this.actualPlace = window.location.pathname.split('/');
//only condition see if not logged
if(this.actualPlace[1] == 'path'){
this.controlKey.push('key');
}
else{
if(!this.loginService.user.ID){
if(localStorage.getItem('user')){
this.loginService.setUser(JSON.parse(localStorage.getItem('user')));
}
else{
this.router.navigateByUrl('/login');
return;
}
}
this.controlKey.push('standardUser');
if(this.loginService.user.userType == 'admin'
|| this.loginService.user.userType == 'superAdmin'){
this.controlKey.push('administration');
}
}
this.menuComponent = res.body.menu.filter(voce => {
return this.controlKey.filter(key => key == voce.Auth).length > 0
});
console.log(this.menuComponent)
}
)
}
login(username, password, component){
this.GFService.countThread('P');
this.loginService.login(username, password).subscribe(
(res: any) => {
this.GFService.countThread('M');
if(res.error){
component.setError(res.error);
}
else{
this.loginService.setUser(res.body.user);
this.router.navigateByUrl('/Homepage');
this.updateMenu();
}
}
)
}
updateMenu(){
this.GFService.countThread('P');
this.GFService.getMenu().subscribe(
(res: any) => {
this.GFService.countThread('M');
this.actualPlace = window.location.pathname.split('/');
if(this.actualPlace[1] == 'path'){
this.controlKey.push('key');
this.view = true;
}
else{
if(!this.loginService.user.ID){
if(localStorage.getItem('user')){
this.loginService.setUser(JSON.parse(localStorage.getItem('user')));
}
else{
this.router.navigateByUrl('/login');
return;
}
}
this.controlKey.push('standardUser');
if(this.loginService.user.userType == 'admin'
|| this.loginService.user.userType == 'superAdmin'){
this.controlKey.push('administration');
}
}
this.menuComponent = res.body.menu.filter(voce => {
return this.controlKey.filter(key => key == voce.Auth).length > 0
});
console.log(this.menuComponent)
}
)
}
menuComponent:Array=[];
开=假;
实际位置=[];
controlKey=[];
建造师(
专用路由器:路由器,
公共服务:通用功能服务,
公共登录服务:登录服务,
) {
}
恩戈尼尼特(){
这个.GFService.countThread('P');
此.GFService.getMenu().subscribe(
(res:any)=>{
这个.GFService.countThread('M');
this.actualPlace=window.location.pathname.split('/');
//如果未记录,则仅查看条件
if(this.actualPlace[1]=“path”){
这个.controlKey.push('key');
}
否则{
如果(!this.loginService.user.ID){
if(localStorage.getItem('user')){
this.loginService.setUser(JSON.parse(localStorage.getItem('user'));
}
否则{
this.router.navigateByUrl('/login');
返回;
}
}
这个.controlKey.push('standardUser');
如果(this.loginService.user.userType=='admin'
||this.loginService.user.userType==“超级管理员”){
这个.controlKey.push(“管理”);
}
}
this.menuComponent=res.body.menu.filter(voce=>{
返回此.controlKey.filter(key=>key==voce.Auth).length>0
});
console.log(this.menuComponent)
}
)
}
登录(用户名、密码、组件){
这个.GFService.countThread('P');
this.loginService.login(用户名、密码)。订阅(
(res:any)=>{
这个.GFService.countThread('M');
if(res.error){
组件设置错误(res.error);
}
否则{
this.loginService.setUser(res.body.user);
this.router.navigateByUrl('/Homepage');
this.updateMenu();
}
}
)
}
updateMenu(){
这个.GFService.countThread('P');
此.GFService.getMenu().subscribe(
(res:any)=>{
这个.GFService.countThread('M');
this.actualPlace=window.location.pathname.split('/');
if(this.actualPlace[1]=“path”){
这个.controlKey.push('key');
this.view=true;
}
否则{
如果(!this.loginService.user.ID){
if(localStorage.getItem('user')){
this.loginService.setUser(JSON.parse(localStorage.getItem('user'));
}
否则{
this.router.navigateByUrl('/login');
返回;
}
}
这个.controlKey.push('standardUser');
如果(this.loginService.user.userType=='admin'
||this.loginService.user.userType==“超级管理员”){
这个.controlKey.push(“管理”);
}
}
this.menuComponent=res.body.menu.filter(voce=>{
返回此.controlKey.filter(key=>key==voce.Auth).length>0
});
console.log(this.menuComponent)
}
)
}
menu.html
<div class="app-menu piston" [ngClass]="{'openMenu': open}" *ngIf="menuComponent.length > 0">
<div class="menuIconDiv piston" [ngClass]="{'bigIcon': open}">
<mat-icon class="menuIcon" (click)="enlargeMenu()">
menu
</mat-icon>
</div>
<div class="menuIconDiv piston"
[ngClass]="{'bigIcon': open, 'inPage': voice.usePath.replace('/', '') === actualPlace[1]}"
*ngFor="let voice of menuComponent"
(click)="action(voice)">
<mat-icon class="menuIcon" >
{{voice.iconText}}
</mat-icon>
<div class='label piston' [ngClass]="{'hide': !open}">{{voice.label}}</div>
</div>
</div>
菜单
{{voice.iconText}
{{voice.label}
有人能帮我吗?
我不明白问题是什么,也是因为在其他应用程序中,我没有遇到过任何此类问题。您是否使用
菜单.component.ts中的onPush
策略?我不知道它是什么。。。所以我认为不要使用它…你能提供stackblitz演示来重现一个问题吗?我创建了一个stackblitz模拟演示,但它工作正常。我不知道这是否是因为它不必使用订阅或什么。由于服务器路径和用户的隐私,我无法在没有模拟的情况下插入完整的工作演示。