更新变量

时间:2016-10-02 14:28:30

标签: angular angular2-directives

我想使用ngstyle来显示或隐藏我的导航栏,它是控制器组件的子组件。

Controller Html

<section>
    <app-nav class="nav" [ngStyle]="{'visibility':isNavShow?'visible':'hidden'}"></app-nav>
    <app-main class="main"></app-main>
</section>

Controller Comp

isNavShow:boolean = false;
subRouterParam:Subscription = new Subscription();

constructor(private accountService: AccountService,private route: ActivatedRoute) {}

ngOnInit() {
    console.log('account id:'+this.accountService.userData.id);
    this.subRouterParam = this.route.params.subscribe(
        (data)=> {
            this.isNavShow = data['isNavShow']==undefined?false:data['isNavShow'];
        }
    );
}

ngDoCheck(){
    console.log(this.isNavShow);
}

ngOnDestroy(){
    this.subRouterParam.unsubscribe();
}

控制器模块

@NgModule({
    declarations: [
        ConsoleComponent,
        NavComponent
    ],
    imports: [
        MainModule,
        CommonModule
    ],
    providers:[TagService]
})
export class ConsoleModule{}

我已记录isNavShow值,我非常确定isNavShow切换值但ngstyle未更新!
奇怪的是,我在开头设置hidden,第一次从hidden切换到visible。但是,即使isNavShow已更改,ngStyle也不会更改。

[update!]我将导航栏更改为此格式,isNavShow确实正确更改。但是ngStyle不起作用..

<div class="nav" [ngStyle]="{'visibility':isNavShow?'visible':'hidden'}">{{isNavShow}}</div>

1 个答案:

答案 0 :(得分:3)

来自路由器的参数只能是字符串,因为它们来自URL。

试试这个:

I found a difference is sbt submit the job can be seen in the cluster management interface, and the shell can not