Getting Started with PrimeNg: 3 Most Useful Components for Your Angular Project
In this article, we'll learn how to use PrimeNG in your angular project from scratch. Since PrimeNG is a massive collection of UI components, we've picked the three most useful ones which any beginner can use in their angular projects. We'll be implementing autocompletion, chart model, and form validation features in this article.PrimeNG is an open-source library consisting of various UI components for angular. It is developed by a firm called PrimeTek Informatics. What makes angular one of the most sought after frontend development framework is, it's component-based design. In traditional web applications, the entire page was reloaded with the requested page. But, with the use of angular, we have something called SPA s (Single Page Applications) wherein, the current page is dynamically rewritten with the new data. That is to say that, a particular component is reloaded with the latest data. So, we can conclude here that angular applications are component extensive . This makes it paramount to design reusable, scalable, and lightweight components. This is where UI component libraries come to picture. They make it easy to use and implement components along with appealing UI for our angular projects. Also, allows you to focus more on the business logic than the UI design, thereby increasing your productivity. In this article, we'll discuss one such library, primeNG , as the title suggests. There are other such libraries like Angular Material , NGX Bootstrap , etc. We'll list a few features of primeNG Learning or implementing primeNG would require the minimum knowledge base in Angular as a prerequisite. Because primeNG is a UI component library, one should know the usage of components in an angular project. I'd also like you to go through the following checklist of the technology stack we'll use. Let's get started by creating an angular project. Fire the following command in your command prompt to create an angular project by name angular-primeng-tutorial . Before we jump into the implementation, we'll install primeNG, which is available in npm. Run the following commands to install primeNG and prime icons, which is a font icon library from the same vendor. And we'll also install angular animations for enhanced user experience. You can run these commands in either command prompt or your VS code terminal. Add the following dependencies in your index.html file. Add the following dependencies in your styles section of the angular.json file. We have added these dependencies for our primeNG module to work fine. Once installed, the components can be made available by importing from primeng/*. Autocompletion is nothing but, suggestions given on typing. Suppose it's a country field and you start to type 'x', the application suggests you countries starting with x. Let's import the ' AutoCompleteModule ' from primeng into our app.module.ts file. This is how my file looks like; Let's go ahead and paste the below code in our app.component.ts file. What we're doing here is we're creating an array string called 'bookGenre' with some data and a function called 'search' triggered by an input event. The function filters your data dropdown list based on the first letter of the user's input. This is how our app.component.html looks. You can see that we've used the autocomplete tag here. It has these attributes: You can further make the [dropdown]="true" as [dropdown]="false" if you don't want a dropdown. Also if you want to make multiple selections, then add [multiple]="true" . The three different types of autocomplete we discussed will look like this: Angular is all about the frontend part of a web application. And most of them deal with the display of information for its users. Not many of us are interested in a long slogging bulletin of information. Instead, short, precise, and pictorial ones are preferred. Charts are one such way to summarise years of data and give subtle insight into what we intend to convey. Charts can be both informative and visually appealing. It is straightforward to use different kinds of charts in primeNG. Here, we'll learn the bar chart and see it's different possible options. First of all, we need to install the npm package by the below command. Mention the same in the scripts section of the angular.json file. Import the module for it to be available for use in the code in your app.module.ts file. Now, in your .html file, type the below code. The tag p-chart signifies the primeNG/chart module tag. And in the type option, we can mention the kind of chart we want to use, bar , pie , line , etc. Data is bound and rendered from the ts file. Type the following code in your corresponding .ts file. Data has labels and datasets in the form of JSON bodies, as seen below. Labels indicate the name of the data you want to compare with. The different data are available in the form of datasets . We can specify the color, width, and other styling options here other than data. We can improvise this further by adding options . With the use of the below code, we can give a title to our chart and mention the position of the chart legend. In .html and .ts files respectively. We can also bind events to the chart elements. This is how we do it: Here, the event trigger is, onDataSelect . When you select any data in the chart, it displays the respective value of it. Paste the below codes in .html and .ts files respectively. This is what our final result looks like! Forms are used in almost every web application, contact us, survey forms, feedback forms, etc. Implementing them using primeNG is more comfortable and comes with a lot of customization and validation options. In this demo, we'll design simple user details form using primeNG. Update the app.module.ts file with the following import statements. We need these modules in our form to function properly. Don't forget to add them to your imports as well. Now, for our user form, we'll consider the first name, last name, password, and description as our input fields. The .html file looks like this. The p-panel tag forms the header part of the form. We ave used the [formGroup] from angular/forms module. We have used the required attribute for mandatory fields. To display messages we have used p-message tag. Also notice that the validations are checked by . valid and . dirty properties. For the password field, we have an additional length validator using [minlength] property. Lastly, the submit button is disabled until the entire form passes all the validations. This is what our .ts file looks like. Here we're making use of the angular form validators, along with required and minlength properties. We're making use of messageService module to display messages. Check the picture below to see the final result! This is how the form validations using primeNG looks like. I hope the article was useful for you! In this article, we learned about the features and usage of primeNG. We implemented three of its most useful components. Without the use of primeNG too, these features can be achieved. But, it's an uphill task when compared to the ease of primeNG. You just have to install the npm package and import the modules you want you to use, and you're good to go! There are a lot of other UI components you can check out and try yourselves. PrimeNG documentation: https://primefaces.org/primeng/showcase/#/ Tutorials: Tutorial Sample 1 , Tutorial Sample 2 .