我有这个枚举(我正在使用TypeScript):
export enum CountryCodeEnum {
France = 1,
Belgium = 2
}
Run Code Online (Sandbox Code Playgroud)
我想在我的表单中构建一个select,每个选项的枚举整数值为value,枚举文本为label,如下所示:
<select>
<option value="1">France</option>
<option value="2">Belgium</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
我使用Typescript创建了一个枚举,用于MyService.service.ts MyComponent.component.ts和MyComponent.component.html.
export enum ConnectionResult {
Success,
Failed
}
Run Code Online (Sandbox Code Playgroud)
我可以轻松地从MyService.service.ts获取并比较定义的枚举变量:
this.result = this.myService.getConnectionResult();
switch(this.result)
{
case ConnectionResult.Failed:
doSomething();
break;
case ConnectionResult.Success:
doSomething();
break;
}
Run Code Online (Sandbox Code Playgroud)
我还想使用枚举在我的HTML中使用*ngIf语句进行比较:
<div *ngIf="result == ConnectionResult.Success; else failed">
<img src="../../assets/connection-success.png" height="300px" class="image-sign-style" />
</div>
<ng-template #failed>
<img src="../../assets/connection-failed.png" height="300px" class="image-sign-style" />
</ng-template>
Run Code Online (Sandbox Code Playgroud)
代码编译但浏览器给我一个错误:
无法读取未定义的属性
使用以下html指示错误行:
有谁知道为什么不能像这样接近枚举?
我想知道以下是否可能以某种方式.问题不仅限于console.info所有Javascript函数.
<a (click)="console.info(foo)">click me doesn't work</a>
Run Code Online (Sandbox Code Playgroud)
Cannot read property 'info' of undefined
模板似乎只能访问组件属性,所以你必须为你的内部创建一个额外的功能Component:
<a (click)="executeConsole(val)">execute console does work</a>
executeConsole(val) {
console.info(val);
}
Run Code Online (Sandbox Code Playgroud)
使用React,您可以执行以下操作:
<a onClick={() => console.info('it works')}>it works</a>
Run Code Online (Sandbox Code Playgroud) 我想将我的枚举显示为字符串,但将其显示为数字。
我正在从Web服务接收json对象,并将其映射到我的Typescript对象
getProperties(): Observable<Property[]> {
return this.http.get<Property[]>(this.getallagentsproperties + '1');
}
export enum LetType {
Notspecified = 0,
LongTerm = 1,
ShortTerm = 2,
Commercial = 4
}
export class Property {
...other stuff...
letType: LetType;
...other stuff...
}
Run Code Online (Sandbox Code Playgroud)
我的组件会进行调用并将其添加到相关属性中
import { Component, OnInit } from '@angular/core';
import { Property } from './Property';
import { PropertyService } from '../properties.service';
@Component({
selector: 'app-properties',
templateUrl: './properties.component.html',
})
export class PropertiesComponent implements OnInit {
properties: Property[];
constructor(private propertyService: PropertyService) { }
ngOnInit() {
this.getProperties() …Run Code Online (Sandbox Code Playgroud)