Angular 4实例变量在方法

时间:2017-08-17 14:44:11

标签: angular variables typescript instance

我有这段代码

@Component({
  selector: 'unb-navbar',
  templateUrl: './navbar.html'
})
 export class NavbarComponent implements OnInit  {

@Input() brand: string;

controlador:boolean=false;
overlay:string="";

@Input() menus:any = [{
  name: 'Início',
  icon: 'home',
  router: 'home',
  path:'home'
}];

private html: string ='';


constructor(private http: Http, private router: Router, private authenticationService: AuthenticationService, private navbarService: NavbarService){

}

ngOnInit() {
    //this.carregarMenuDinamico();
}


carregarMenuDinamico(){
    this.authenticationService.getUrlFromBarramento()
        .subscribe(res=> {
                this.navbarService.createTemplate (AuthenticationService.base_url)
                    .subscribe (res => {
                        this.html = res;
                    });
            },
            error => {
                this.authenticationService.getUrlFromConfig()
                    .subscribe(response =>{
                        this.navbarService.createTemplate (AuthenticationService.base_url)
                            .subscribe (res => { 
                                this.html = res;
                            });
                    });
            });
}

carregarBars(){
  if(this.controlador){
    this.controlador =false;
    this.overlay = "none";
  }else{
    this.controlador =true;
    this.overlay = "block";
  }

}

}  

当此行执行this.html = res; this.html总是未定义,我不知道为什么。

代码使用webpack正确编译,并且此方法在没有错误的情况下运行,但永远不会将任何值赋予this.html。

我尝试放置一个console.log,但这种情况发生了:

这个东西在chrome中调试

debug in chrome

但是这个东西appen当coneols.log这个变量

use console.log in res and this.html

但没有任何变化。而且我不知道为什么。

1 个答案:

答案 0 :(得分:0)

 this.authenticationService.getUrlFromBarramento()
        .subscribe(res=> {
                this.navbarService.createTemplate (AuthenticationService.base_url)
                    .subscribe (res => {
                        this.html = res;
                    });
            },

这可能是微不足道的,但是当你在订阅getUrlFromBarramento()和res2或其他用于createTemplate的东西时,当你链接两个observables如res1时,我认为你需要使用两个不同的变量来进行回调。