ng-container ng-for ng-if 数据类型

本文介绍了一个使用Angular框架实现的简单示例,展示了如何利用*ngFor指令进行列表数据的渲染,并通过*ngIf指令控制元素的显示与隐藏。示例中定义了一个包含三个元素的数组list,使用*ngFor循环遍历数组并将符合条件的前两项数据展示出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import {Component} from '@angular/core'

@Component({
    selector:'demo03',
    template:`
        <h1>这是demo03</h1>
        <ul>
            <ng-container *ngFor="let tmp of list;let myIndex=index">
                <li *ngIf="myIndex<2">
                    {{"index is "+myIndex+" value is "+tmp}}
                </li>
            </ng-container>
        </ul>
        <p *ngIf="flag">test</p>
    `,
})

export class Demo03Component{
    /*
        ts 指定数据类型
        count:number=1
        hasMore:boolean=true
    */
    list:Array<any>=[100,200,300];
    flag:boolean=false;
} 

在html的Details、Progress Bar输入内容时,this.steps的iStep_Description、progress_bar是空值,如何修改代码 stepper的html如下: <div id="multi-step-form-container"> <nb-card> <nb-card-body> <nb-stepper #stepper> <ng-container *ngFor="let step of steps; let i = index"> <nb-step [stepControl]="step" [label]="'Step ' + (i + 1) + ': ' + (i === 0 ? 'Step1' : i === 1 ? 'Step2' : 'Step3' + (i > 2 ? '' : ''))"> <h6 style="margin-top: 3rem;" class="font-normal"><span style="color: red;">*</span>Step/Item #{{i+1}} Range Date</h6> <ul> <input nbInput placeholder="Project Date Range" [nbDatepicker]="formpicker" [(ngModel)]="steps[i].iDate"> <nb-rangepicker #formpicker (rangeChange)="handleRangeChange($event)"></nb-rangepicker> </ul> <h6 style="margin-top: 3rem;" class="font-normal"><span style="color: red;">*</span>Step/Item #{{i+1}} Progress Bar</h6> <ul> <div class="mt-3" style="display: flex; align-items: center;"> <nb-action icon="minus-outline" (click)="setValue(value-1)" style="margin-right: 8px;"></nb-action> <nb-progress-bar [value]="steps[i].progress_bar" [status]="getStatus(steps[i].progress_bar)" [displayValue]="true" style="flex-grow: 1;"></nb-progress-bar> <nb-action icon="plus-outline" (click)="setValue(value+10)" style="margin-left: 8px;"></nb-action> </div> </ul> <form [formGroup]="step" (ngSubmit)="onSubmit(i)" class="step-container"> <h6 style="margin-top: 3rem;"class="font-normal"><span style="color: red;">*</span>Step/Item #{{i+1}} Details</h6> <ul> <div class="input-group"> <textarea rows="5" nbInput fullWidth [(ngModel)]="steps[i].iStep_Description" [formControlName]="i === 0 ? 'firstCtrl' : i === 1 ? 'secondCtrl' : 'thirdCtrl' + (i > 2 ? (i - 2).toString() : '')"[ngClass]="{'form-control-danger': step.invalid && (step.dirty || step.touched)}"></textarea> </div> </ul> <button style="margin-right: 10px;" nbButton nbStepperPrevious *ngIf="i > 0" status="info" outline>Prev</button> <button style="margin-right: 10px;" nbButton nbStepperNext *ngIf="i < steps.length - 1" status="primary" outline>Next</button> <button style="margin-right: 10px;" nbButton [disabled]="i !== steps.length - 1" (click)="onSubmit(i)" status="success" outline>{{ i === steps.length - 1 ? 'Confirm' : 'Submit' }}</button> </form> </nb-step> </ng-container> </nb-stepper> </nb-card-body> <nb-card-footer> <button nbButton (click)="addSteps('New Step', 'newCtrl' + (steps.length).toString())">Add Step</button> </nb-card-footer> </nb-card> </div> stepper对应的component.ts部分函数如下: formGroupArray: FormGroup[] = []; steps: { formGroup: FormGroup, iDate: any, iStep_Description: any, progress_bar: any }[] = []; value = 10; setValue(newValue) { this.value = Math.min(Math.max(newValue, 0), 100) } get status() { if (this.value <= 25) { return 'basic'; } else if (this.value <= 50) { return 'warning'; } else if (this.value <= 75) { return 'info'; } else { return 'success'; } } addSteps(label: string, controlName: string) { const newFormGroup = this.fb.group({ [controlName]: ['', Validators.required], description: [{ value: null, disabled: true }], progress_bar: [{ value: null, disabled: true }], iDate: [{ value: null, disabled: true }], }); this.steps.push({ formGroup: newFormGroup, iDate: null, iStep_Description: null, progress_bar: 0 }); } onSubmit(stepIndex: number) { const step = this.steps[stepIndex]; const stepForm = step.formGroup; stepForm.markAsDirty(); if (stepForm.valid) { this.currentStepIndex = Math.min(stepIndex + 1, this.steps.length - 1); } this.iStep_Description=stepForm.value; }
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值