GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. I tried to use a typescript enum for an array index in a template to create data binding for an HTML attribute in this way:. Correct, as martinmcwhorter states, your component class is basically your execution context in Angular 2 applications.
All properties defined on your component can be interpolated in your component's template. If not, please re-open the issue with a live reproduce scenario.
Please file a new issue if you are encountering a similar or related problem. Read more about our automatic conversation locking policy. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up. New issue. Jump to bottom.
Usage of enums in templates not possible? Copy link Quote reply. It compiles ok with Typescript 1. This comment has been minimized.
Sign in to view. As with Angular 1. Contributor Author. That was the problem! Second, templates cannot access objects that are not exposed by a controller or component.
This is quite different than how enums work in a language like C. This action has been performed automatically by a bot. Sign up for free to subscribe to this conversation on GitHub.
Already have an account? Sign in. Linked pull requests. You signed in with another tab or window.Enum is a convenient feature offered by TypeScript.
Enum allows you to specify a possible property value from a predefined set of values using meaningful names, instead of the numeric constants that are usually used in this case. For example, we can specify the FlexChart. The only way is to use numeric constants corresponding to the enum members, but this approach produces unclear markup, and it might be annoying to figure out their values:.
The source of the problem is that it is impossible to reference anything except for the component properties in Angular template expressions. We can just add a property that stores a reference to the necessary Enum type, like the ChartType property here:. Note that this problem is not so acute when it comes to Wijmo components, where you can assign the string name of an enum member to a property.
This approach will work, because all Wijmo enum type properties can convert such strings to actual enum values. For example:. But, this approach may cause problems in case of two way binding scenarios. The code that uses true enum values is free of these potential problems:. The whole working example can be found here.
Try Wijmo's TypeScript controls. Stay up to date with the GrapeCity feeds. For more information, contact us at: press grapecity. Search Cart My Account. NET Web Forms. Resources Documentation Technical Support Blogs. Back Spread Products Spread. NET Enum properties in Angular templates The whole working example can be found here.
Your template can only access member variables of its component class. So, if you want to use the enum's values, assign it the Enum to a member variable. Since you are using [ ngModel ] on input element, you cannot assign to [value] property of input element. Learn more. Angular2 use enum value in html value attribute Ask Question. Asked 3 years, 1 month ago. Active 1 year, 10 months ago. Viewed 23k times. Active Oldest Votes.
Is that a right practise to do? I would not say so. I use a variation of this technique regularly where I have an object whose properties are strings that I treat as constants, and I would attach that object to various components as needed. Well, should a component contain a property containing an enum? Is that right from design?
I do not feel so even though I know it is the only way to be able to use enum in templates. The other approach would be methods designed to work with the enum in the component accessible from the template and that is the approach I see as correct. I hear your point about a component not including an enum as a property, especially since the enum doesn't technically belong to the component class from a design perspective.
But my view on this specific matter is that for the most part, the role of the component class is to expose data and functionality to the template for its use.
Yes, you can wrap the enum in a method to isolate it more but I'm not sure the extra ceremony buys you anything except more boilerplate code. An enum is just a list of constants after all What's the risk of change? Aravind Aravind The proposal is to create an enum that uses string values as opposed to the currently used numeric values.
After TypeScript 2. While the term pattern might be a bit of an oversell, this technique or trick will get you the intended result.
Included is an example to demonstrate the usefulness of this solution. The article compares other TypeScript features that don't quite meet the mark and also discusses how the solution works to create the right combination of type checking and runtime behavior. The TypeScript team might still make it easier to create 'string enums' in future versions of the language. Regardless, learning how this solution works will deepen your understanding of the new TypeScript 2.
If you think about inputs such as dropdowns or radio buttons where the user must select a single value from multiple choices, the underlying values oftentimes map nicely to an enum data structure.
For example, consider a selection of shirt sizes. You could easily define the shirt sizes with an enum:. This is a nice data structure with which to code. All of the related values are in one place and it's easy to access a value from the list. To understand what TypeScript is doing, it compiles the enum to a data structure at runtime that looks like this:. Looking at the first two lines, you see the underlying values for these enum choices are numbers.
However in the last line, you see it is possible to get the enum selection as a string. If you define a property that accepts one of the enum values as a string, the type of that property will be string which has no association with the enum from which you would like to get the value.
TypeScript, however does offer a way to limit string values but this is a separate type definition altogether:. Now think back to the multi-choice selections in HTML like the dropdown and radio buttons.
The HTML elements define values using attributes which are strings. So wouldn't it be great if you could get the simplicity of an enum but with string values instead of numeric values? The SizeSelectComponent demonstrates this use case. Its template contains a select element with several options defined, in this case it displays a range of shirt sizes.
Notice the Size variable and the Size type defined. These declarations create the 'string enum' definition. Furthermore, notice the selectedSize input property for this component.
Notice it is of type Size.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
TypeScript String Enums
Already on GitHub? Sign in to your account. But with the numeric value it works. Is it possible to use the enum expression? This is by design I think See This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem. Read more about our automatic conversation locking policy. Skip to content.
Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up.
New issue. Jump to bottom. Copy link Quote reply. Hello, I have a problem with using typescript enums in angular2 templates. This comment has been minimized. Sign in to view.Enums or enumerations are a new data type supported in TypeScript. Most object-oriented languages like Java and C use enums. This is now available in TypeScript too. In simple words, enums allow us to declare a set of named constants i.
Enums can be defined using the keyword enum. Let's say we want to store a set of print media types. The corresponding enum in TypeScript would be:. In the above example, we have an enum named PrintMedia. The enum has four values: Newspaper, Newsletter, Magazine, and Book. Here, enum values start from zero and increment by 1 for each member.
It would be represented as:. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1. We also have the option to initialize the first numeric value ourselves.
For example, we can write the same enum as:. The first member, Newspaper, is initialized with the numeric value 1. The remaining members will be incremented by 1 from the numeric value of the first value. Thus, in the above example, Newsletter would be 2, Magazine would be 3 and Book would be 4. In the above example, we declared an enum PrintMedia. Next, we declare a function getMedia that takes in an input parameter mediaName of the type string. This function returns an enum PrintMedia. In the function, we check for the type of media.
If the media name matches 'Forbes' or 'Outlook', we return enum member PrintMedia. Numeric enums can include members with computed numeric value. The value of an enum member can be either a constant or computed. The following enum includes members with computed values. When the enum includes computed and constant members, then uninitiated enum members either must come first or must come after other initialized members with numeric constants.
The following will give an error. String enums are similar to numeric enums, except that the enum values are initialized with string values rather than numeric values. The benefits of using string enums is that string enums offer better readability.
If we were to debug a program, it is easier to read string values rather than numeric values. In the above example, we have defined a string enum, PrintMedia, with the same values as the numeric enum above, with the difference that these enum values are initialized with string literals. The difference between numeric and string enums is that numeric enum values are auto-incremented, while string enum values need to be individually initialized.
Enum in TypeScript supports reverse mapping. It means we can access the value of a member and also a member name from its value. Consider the following example. As you can see in the above example, PrintMedia returns its member name "Magazine". This is because of reverse mapping.As we know the benefit of constant and enum in any other language, we need to it wherever need in our project. But angularjs does not have any enum concept, we will try to create some constants, object to achieve the same goal of enum easily with example.
Luckily AngularJS provides a constant service type that can be used for static data that will not change and can be used throughout the application. This can be injected like any other service into any module where it is required. Wherever we need to use this Constants we simply inject and use it, Let add a Roles list of constants. Let's say I want to use this constant into my contact controller then inject 'Constants' and use it like any other object.
By using this constant service can we create the enum, let's see this with example, suppose we want to create enum of Gender like this in C. How can we use this Gender enum, It can be used in the same we as we used our Roles constant, see this.
So far it was simple, only the name value or Id value but suppose I have a list of countries with Id, short name and long name an want to access by Id or short name to get the long name then it will be a little bit tricky, let's see what I am talking.
As you can see the full name have space in some country names, even if we will try to create the enum in C or any other language, we would need to define the enum name as well as the description.
If want want to use by Id then simply we can use the index be reducing one in the value because index start from zero, which is not the good example but if it can help anyone, like this. If any value match till then it's good, if not, it will through error, so rest of the code after this line will not execute which we cannot afford in any case. So what is the better way to use this kind of complicated enum or constant? If you have ever worked on any live project in angularjs, we always have some application level service, right?
We will inject this constant in that service and will create a simple method like find full name by short name or find full name by Id or find short name by id etc. Since we already injecting our application service in different controller so no need to inject the Constants any more we can use the application service wherever needed like this.
Let us know if you have any challenging structure to use as a constant. There might be some cases where you can get the data from database and use as constant, in that case, better to save those records in local storage as it is and write some useful methods and use them directly form there.
Angularjs Constant and enum with example. Country[someId - 1]. ShortName OR Constants. FullName If any value match till then it's good, if not, it will through error, so rest of the code after this line will not execute which we cannot afford in any case. Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
(Angular & ASP.NET) Gurustop.NET By @Meligy
Other blogs you may like Angularjs powerful paging on table and searching with delay and demo Paging on table is the basic requirement and most of us look for some grid so it can provide the complete feature of paging and sorting but in Angularjs it is so easy that anyone can achieve it by writing some code.
I checked and found most of the available code give the paging feature in for 1 to In Angularjs ng-change is the best event to hook the function to get the second dropdown list items.Part 4 - Working with Enums and Arrays in Typescript
In this article, we will try to fill the state dropdown on selection change of country and then fill the city dropdown on change In this article we will try to open the modal popup to add new customer and edit an existing customer record. I divided article in two parts because of the There are already too many directive have been created by experts but as a developer we need to write our own to achieve our specific goal. We will use enetity framework code first to save and retrieve data from database.
Config 2 Routing 3.