这是任务:
您的任务是创建一个名为 TestComponent 的简单 Angular 组件,该组件由选择器 test-component 标识
该组件必须具有名为 inputData 的数组类型的输入属性。
如果没有输入数据(空或丢失),您必须在 id="noData"的 div 中写入“无数据”。如果输入
给定此 div 不得出现在页面的 DOM 中。
如果给定了输入,则必须将输入数组的元素写入单独的 div。 div 必须是
id="dataList"的 div 的子级。如果任何给定字符串的长度为奇数,则文本颜色应为红色。
如果它是均匀的,颜色应该是绿色的。
这就是我所做的:
import { Component, NgModule, EventEmitter, Output, Input } from '@angular/core';
@Component({
selector: 'app-test-component',
template: `
<test-component>
<div id="noData" *ngIf=!'inputData'>No data</div>
<div id="dataList"></div>
</test-component>
<test-component>
<div id="dataList" *ngIf='inputData'>
<div *ngFor="let item of inputData; odd as isOdd; even as isEven;[ngClass]="{even: isEven, odd: isOdd}">
<div>{{item}}</div>
</div>
</div>
</test-component>
`,
style: '.even {
background-color: read;
color: white;
}
.odd {
background-color: gren;
color: white;
}'
})
export class TestComponent{
@Input() inputData: Array<string>;
}
我的怀疑是尊重!在 ngIf 上,从 Dom 中取出元素和奇数,偶数。这些是我还没有的部分。你能给我一些更正吗?
最佳答案
您的 bang
/!
是在错误的地方。它应该在引号内。
<div id="noData" *ngIf="!inputData!>No data</div>
isOdd 或 isEven 的来源是什么?这是输入字段还是您需要计算它?你可以做一些像...<div *ngFor="let item of inputData; index as index;" [ngClass]="{even: index % 2 === 0, odd: index % 2 !== 0}">
关于棱 Angular 分明!和奇偶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68443262/