How can I properly bind an enum within a dropdown menu in Angular 4?
up vote
0
down vote
favorite
In a dropdown menu, I've used an Enum data structure (Typescript) to store my values. However it seems that the data binding to the object's category field doesn't take place properly and nothing is selected by default. Is there any better solution?
export enum CategoryEnum {
EXAMPLECAT1 = 1,
EXAMPLECAT2 = 2,
EXAMPLECAT3 = 3,
EXAMPLECAT4 = 4
}
@Component({
selector: 'category',
template: `
<label for="appCategory">Example Category: </label>
<select class="form-control" id="exampleCategory" required
(change)="parseValue($event.target.value)"
(ngModelChange)="changeSelectedType($event)">
<option *ngFor="let category of categoryTypes"
[value]="category">{{category.type}}</option>
</select>
`
})
export class CategoryComponent {
private selectedCategoryType: CategoryEnum
private categoryTypes;
constructor(){
this.categoryTypes = CategoryMapping
}
parseValue(value : string) {
this.selectedCategoryType = AppCategoryEnum[value];
}
//Logging: Change Selected Product type callback
private changeSelectedType(event: any) {
console.log(event); //object, depends on ngValue
console.log(this.selectedCategoryType); //object, depends on ngValue
}
}
Mapping the enum data type to labels:
export const CategoryMapping = [
{ value: ExampleCategoryEnum.EXAMPLECAT1, type: 'Example 1' },
{ value: ExampleCategoryEnum.EXAMPLECAT2, type: 'Example 2'},
{ value: ExampleCategoryEnum.EXAMPLECAT3, type: 'Example 3'},
{ value: ExampleCategoryEnum.EXAMPLECAT4, type: 'Example 4'}
];
angular typescript data-binding enums
add a comment |
up vote
0
down vote
favorite
In a dropdown menu, I've used an Enum data structure (Typescript) to store my values. However it seems that the data binding to the object's category field doesn't take place properly and nothing is selected by default. Is there any better solution?
export enum CategoryEnum {
EXAMPLECAT1 = 1,
EXAMPLECAT2 = 2,
EXAMPLECAT3 = 3,
EXAMPLECAT4 = 4
}
@Component({
selector: 'category',
template: `
<label for="appCategory">Example Category: </label>
<select class="form-control" id="exampleCategory" required
(change)="parseValue($event.target.value)"
(ngModelChange)="changeSelectedType($event)">
<option *ngFor="let category of categoryTypes"
[value]="category">{{category.type}}</option>
</select>
`
})
export class CategoryComponent {
private selectedCategoryType: CategoryEnum
private categoryTypes;
constructor(){
this.categoryTypes = CategoryMapping
}
parseValue(value : string) {
this.selectedCategoryType = AppCategoryEnum[value];
}
//Logging: Change Selected Product type callback
private changeSelectedType(event: any) {
console.log(event); //object, depends on ngValue
console.log(this.selectedCategoryType); //object, depends on ngValue
}
}
Mapping the enum data type to labels:
export const CategoryMapping = [
{ value: ExampleCategoryEnum.EXAMPLECAT1, type: 'Example 1' },
{ value: ExampleCategoryEnum.EXAMPLECAT2, type: 'Example 2'},
{ value: ExampleCategoryEnum.EXAMPLECAT3, type: 'Example 3'},
{ value: ExampleCategoryEnum.EXAMPLECAT4, type: 'Example 4'}
];
angular typescript data-binding enums
You have to use[ngValue]for options when you want to send non-primitive types (in your case, objects)
– trichetriche
Nov 20 at 9:18
And to select a value as default, you have to set yourngModel(which you lack on your select) to the default value you want to use.
– trichetriche
Nov 20 at 9:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In a dropdown menu, I've used an Enum data structure (Typescript) to store my values. However it seems that the data binding to the object's category field doesn't take place properly and nothing is selected by default. Is there any better solution?
export enum CategoryEnum {
EXAMPLECAT1 = 1,
EXAMPLECAT2 = 2,
EXAMPLECAT3 = 3,
EXAMPLECAT4 = 4
}
@Component({
selector: 'category',
template: `
<label for="appCategory">Example Category: </label>
<select class="form-control" id="exampleCategory" required
(change)="parseValue($event.target.value)"
(ngModelChange)="changeSelectedType($event)">
<option *ngFor="let category of categoryTypes"
[value]="category">{{category.type}}</option>
</select>
`
})
export class CategoryComponent {
private selectedCategoryType: CategoryEnum
private categoryTypes;
constructor(){
this.categoryTypes = CategoryMapping
}
parseValue(value : string) {
this.selectedCategoryType = AppCategoryEnum[value];
}
//Logging: Change Selected Product type callback
private changeSelectedType(event: any) {
console.log(event); //object, depends on ngValue
console.log(this.selectedCategoryType); //object, depends on ngValue
}
}
Mapping the enum data type to labels:
export const CategoryMapping = [
{ value: ExampleCategoryEnum.EXAMPLECAT1, type: 'Example 1' },
{ value: ExampleCategoryEnum.EXAMPLECAT2, type: 'Example 2'},
{ value: ExampleCategoryEnum.EXAMPLECAT3, type: 'Example 3'},
{ value: ExampleCategoryEnum.EXAMPLECAT4, type: 'Example 4'}
];
angular typescript data-binding enums
In a dropdown menu, I've used an Enum data structure (Typescript) to store my values. However it seems that the data binding to the object's category field doesn't take place properly and nothing is selected by default. Is there any better solution?
export enum CategoryEnum {
EXAMPLECAT1 = 1,
EXAMPLECAT2 = 2,
EXAMPLECAT3 = 3,
EXAMPLECAT4 = 4
}
@Component({
selector: 'category',
template: `
<label for="appCategory">Example Category: </label>
<select class="form-control" id="exampleCategory" required
(change)="parseValue($event.target.value)"
(ngModelChange)="changeSelectedType($event)">
<option *ngFor="let category of categoryTypes"
[value]="category">{{category.type}}</option>
</select>
`
})
export class CategoryComponent {
private selectedCategoryType: CategoryEnum
private categoryTypes;
constructor(){
this.categoryTypes = CategoryMapping
}
parseValue(value : string) {
this.selectedCategoryType = AppCategoryEnum[value];
}
//Logging: Change Selected Product type callback
private changeSelectedType(event: any) {
console.log(event); //object, depends on ngValue
console.log(this.selectedCategoryType); //object, depends on ngValue
}
}
Mapping the enum data type to labels:
export const CategoryMapping = [
{ value: ExampleCategoryEnum.EXAMPLECAT1, type: 'Example 1' },
{ value: ExampleCategoryEnum.EXAMPLECAT2, type: 'Example 2'},
{ value: ExampleCategoryEnum.EXAMPLECAT3, type: 'Example 3'},
{ value: ExampleCategoryEnum.EXAMPLECAT4, type: 'Example 4'}
];
angular typescript data-binding enums
angular typescript data-binding enums
edited Nov 20 at 11:25
Mohammed Ameen
475
475
asked Nov 20 at 9:12
Janina Alisár
13
13
You have to use[ngValue]for options when you want to send non-primitive types (in your case, objects)
– trichetriche
Nov 20 at 9:18
And to select a value as default, you have to set yourngModel(which you lack on your select) to the default value you want to use.
– trichetriche
Nov 20 at 9:20
add a comment |
You have to use[ngValue]for options when you want to send non-primitive types (in your case, objects)
– trichetriche
Nov 20 at 9:18
And to select a value as default, you have to set yourngModel(which you lack on your select) to the default value you want to use.
– trichetriche
Nov 20 at 9:20
You have to use
[ngValue] for options when you want to send non-primitive types (in your case, objects)– trichetriche
Nov 20 at 9:18
You have to use
[ngValue] for options when you want to send non-primitive types (in your case, objects)– trichetriche
Nov 20 at 9:18
And to select a value as default, you have to set your
ngModel (which you lack on your select) to the default value you want to use.– trichetriche
Nov 20 at 9:20
And to select a value as default, you have to set your
ngModel (which you lack on your select) to the default value you want to use.– trichetriche
Nov 20 at 9:20
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
We can simplify the bind with ngModel, I have create another property to bind with select element and get property to get the vaLue as CategoryEnum
compoenent
// I have change this to public just for demo
public get selectedCategoryType():CategoryEnum {
return this.selectedValue ? this.selectedValue.value: null;
}
private categoryTypes;
public selectedValue:any;
constructor() {
this.categoryTypes = CategoryMapping;
this.selectedValue = this.categoryTypes[2]; // set default value
}
template
<select class="form-control" id="exampleCategory" required [(ngModel)]="selectedValue" >
<option></option>
<option *ngFor="let category of categoryTypes"
[ngValue]="category">{{category.type}}</option>
</select>
<br>
selectedCategoryType : {{selectedCategoryType | json}}
You have to use [ngValue] for options when you want to send non-primitive types (in your case, objects) as @trichetriche said
demo
add a comment |
up vote
-1
down vote
Yes, set
<select ... [value]="1">
where 1 is example default category enum value.
And use category.value instead whole object category as option value
<option ... [value]="category.value" >
and update your .ts file to handle that change.
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
We can simplify the bind with ngModel, I have create another property to bind with select element and get property to get the vaLue as CategoryEnum
compoenent
// I have change this to public just for demo
public get selectedCategoryType():CategoryEnum {
return this.selectedValue ? this.selectedValue.value: null;
}
private categoryTypes;
public selectedValue:any;
constructor() {
this.categoryTypes = CategoryMapping;
this.selectedValue = this.categoryTypes[2]; // set default value
}
template
<select class="form-control" id="exampleCategory" required [(ngModel)]="selectedValue" >
<option></option>
<option *ngFor="let category of categoryTypes"
[ngValue]="category">{{category.type}}</option>
</select>
<br>
selectedCategoryType : {{selectedCategoryType | json}}
You have to use [ngValue] for options when you want to send non-primitive types (in your case, objects) as @trichetriche said
demo
add a comment |
up vote
0
down vote
We can simplify the bind with ngModel, I have create another property to bind with select element and get property to get the vaLue as CategoryEnum
compoenent
// I have change this to public just for demo
public get selectedCategoryType():CategoryEnum {
return this.selectedValue ? this.selectedValue.value: null;
}
private categoryTypes;
public selectedValue:any;
constructor() {
this.categoryTypes = CategoryMapping;
this.selectedValue = this.categoryTypes[2]; // set default value
}
template
<select class="form-control" id="exampleCategory" required [(ngModel)]="selectedValue" >
<option></option>
<option *ngFor="let category of categoryTypes"
[ngValue]="category">{{category.type}}</option>
</select>
<br>
selectedCategoryType : {{selectedCategoryType | json}}
You have to use [ngValue] for options when you want to send non-primitive types (in your case, objects) as @trichetriche said
demo
add a comment |
up vote
0
down vote
up vote
0
down vote
We can simplify the bind with ngModel, I have create another property to bind with select element and get property to get the vaLue as CategoryEnum
compoenent
// I have change this to public just for demo
public get selectedCategoryType():CategoryEnum {
return this.selectedValue ? this.selectedValue.value: null;
}
private categoryTypes;
public selectedValue:any;
constructor() {
this.categoryTypes = CategoryMapping;
this.selectedValue = this.categoryTypes[2]; // set default value
}
template
<select class="form-control" id="exampleCategory" required [(ngModel)]="selectedValue" >
<option></option>
<option *ngFor="let category of categoryTypes"
[ngValue]="category">{{category.type}}</option>
</select>
<br>
selectedCategoryType : {{selectedCategoryType | json}}
You have to use [ngValue] for options when you want to send non-primitive types (in your case, objects) as @trichetriche said
demo
We can simplify the bind with ngModel, I have create another property to bind with select element and get property to get the vaLue as CategoryEnum
compoenent
// I have change this to public just for demo
public get selectedCategoryType():CategoryEnum {
return this.selectedValue ? this.selectedValue.value: null;
}
private categoryTypes;
public selectedValue:any;
constructor() {
this.categoryTypes = CategoryMapping;
this.selectedValue = this.categoryTypes[2]; // set default value
}
template
<select class="form-control" id="exampleCategory" required [(ngModel)]="selectedValue" >
<option></option>
<option *ngFor="let category of categoryTypes"
[ngValue]="category">{{category.type}}</option>
</select>
<br>
selectedCategoryType : {{selectedCategoryType | json}}
You have to use [ngValue] for options when you want to send non-primitive types (in your case, objects) as @trichetriche said
demo
answered Nov 20 at 10:00
malbarmawi
4,95831131
4,95831131
add a comment |
add a comment |
up vote
-1
down vote
Yes, set
<select ... [value]="1">
where 1 is example default category enum value.
And use category.value instead whole object category as option value
<option ... [value]="category.value" >
and update your .ts file to handle that change.
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
add a comment |
up vote
-1
down vote
Yes, set
<select ... [value]="1">
where 1 is example default category enum value.
And use category.value instead whole object category as option value
<option ... [value]="category.value" >
and update your .ts file to handle that change.
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Yes, set
<select ... [value]="1">
where 1 is example default category enum value.
And use category.value instead whole object category as option value
<option ... [value]="category.value" >
and update your .ts file to handle that change.
Yes, set
<select ... [value]="1">
where 1 is example default category enum value.
And use category.value instead whole object category as option value
<option ... [value]="category.value" >
and update your .ts file to handle that change.
edited Nov 20 at 9:21
answered Nov 20 at 9:18
Kamil Kiełczewski
8,27375586
8,27375586
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
add a comment |
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
She is using objects as values, not numbers
– trichetriche
Nov 20 at 9:20
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
I update answer
– Kamil Kiełczewski
Dec 4 at 20:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389635%2fhow-can-i-properly-bind-an-enum-within-a-dropdown-menu-in-angular-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have to use
[ngValue]for options when you want to send non-primitive types (in your case, objects)– trichetriche
Nov 20 at 9:18
And to select a value as default, you have to set your
ngModel(which you lack on your select) to the default value you want to use.– trichetriche
Nov 20 at 9:20