*ngIf 在我的 Html 中不起作用,但 ngIf 显示第一个内容
*ngIf is not working in my Html but ngIf showing first content
我看到很多关于这个问题的问题。但他们对我没有帮助。
这是我的Html
<div class="pl-lg-4">
<div *ngIf="isStorySelected; else hi" class="row">
<div class="col-lg-12">
<div class="form-group">
<label class="form-control-label" for="input-username">Story Name</label>
<label class="form-control-label" for="input-username">Selected option</label>
</div>
</div>
</div>
<ng-template #hi>
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label class="form-control-label">Select Story</label>
<select class="form-control form-control-alternative" value="">
<option>some option one</option>
<option>some option 2</option>
</select>
</div>
</div>
</div>
</ng-template>
如果我像这样使用 *ngIf,它什么也没有显示。但使用 like ngIf 显示第一个内容。
这是我的组件。
import { Component, OnInit } from '@angular/core';
import { Route } from '@angular/compiler/src/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-create-episodes',
templateUrl: './create-episodes.component.html',
styleUrls: ['./create-episodes.component.scss']
})
export class CreateEpisodesComponent implements OnInit {
public activeRoutes: ActivatedRoute;
public storyId: string;
public isStorySelected = false;
constructor(activeRoutes: ActivatedRoute) {
this.activeRoutes = activeRoutes;
}
ngOnInit() {
this.storyId = this.activeRoutes.snapshot.paramMap.get('id');
if (this.storyId) {
console.log('1');
this.isStorySelected = true;
} else {
console.log('2');
this.isStorySelected = false;
}
}
}
这里我添加了 stackblitz url: https://stackblitz.com/edit/angular-lmxswk?file=src/app/app.component.ts
我已经将 CommonModule 导入到 appModule。
并且我使用的是最新版本的 Angular。 (10)
希望这能解决您的问题。缺少 then
选项导致问题。
<div *ngIf="isStorySelected; then hello; else hi" class="row">
<ng-template #hello>
<h1>Hello</h1>
</ng-template>
<ng-template #hi>
<h1>Hi</h1>
</ng-template>
</div>
的界面
paramMap
return Observable<ParamMap>
, 因此你需要将它转换为 observable
name = 'Angular';
public storyId: Observable<boolean>;
public router: ActivatedRoute;
constructor(router: ActivatedRoute) {
this.router = router;
}
ngOnInit(){
this.storyId = this.router.paramMap.pipe(
map((res) =>{
const currentId = res.get('id');
if(currentId){
return true;
} else {
return false;
}
}));
}
内部模板
<div *ngIf="storyId | async; else hi" class="row"></div>
这里正在工作stackblitz
我看到很多关于这个问题的问题。但他们对我没有帮助。
这是我的Html
<div class="pl-lg-4">
<div *ngIf="isStorySelected; else hi" class="row">
<div class="col-lg-12">
<div class="form-group">
<label class="form-control-label" for="input-username">Story Name</label>
<label class="form-control-label" for="input-username">Selected option</label>
</div>
</div>
</div>
<ng-template #hi>
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label class="form-control-label">Select Story</label>
<select class="form-control form-control-alternative" value="">
<option>some option one</option>
<option>some option 2</option>
</select>
</div>
</div>
</div>
</ng-template>
如果我像这样使用 *ngIf,它什么也没有显示。但使用 like ngIf 显示第一个内容。
这是我的组件。
import { Component, OnInit } from '@angular/core';
import { Route } from '@angular/compiler/src/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-create-episodes',
templateUrl: './create-episodes.component.html',
styleUrls: ['./create-episodes.component.scss']
})
export class CreateEpisodesComponent implements OnInit {
public activeRoutes: ActivatedRoute;
public storyId: string;
public isStorySelected = false;
constructor(activeRoutes: ActivatedRoute) {
this.activeRoutes = activeRoutes;
}
ngOnInit() {
this.storyId = this.activeRoutes.snapshot.paramMap.get('id');
if (this.storyId) {
console.log('1');
this.isStorySelected = true;
} else {
console.log('2');
this.isStorySelected = false;
}
}
}
这里我添加了 stackblitz url: https://stackblitz.com/edit/angular-lmxswk?file=src/app/app.component.ts
我已经将 CommonModule 导入到 appModule。
并且我使用的是最新版本的 Angular。 (10)
希望这能解决您的问题。缺少 then
选项导致问题。
<div *ngIf="isStorySelected; then hello; else hi" class="row">
<ng-template #hello>
<h1>Hello</h1>
</ng-template>
<ng-template #hi>
<h1>Hi</h1>
</ng-template>
</div>
paramMap
return Observable<ParamMap>
, 因此你需要将它转换为 observable
name = 'Angular';
public storyId: Observable<boolean>;
public router: ActivatedRoute;
constructor(router: ActivatedRoute) {
this.router = router;
}
ngOnInit(){
this.storyId = this.router.paramMap.pipe(
map((res) =>{
const currentId = res.get('id');
if(currentId){
return true;
} else {
return false;
}
}));
}
内部模板
<div *ngIf="storyId | async; else hi" class="row"></div>
这里正在工作stackblitz