{"id":570,"date":"2023-09-10T09:56:13","date_gmt":"2023-09-10T15:56:13","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=570"},"modified":"2023-09-11T09:20:56","modified_gmt":"2023-09-11T15:20:56","slug":"angular-commands","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/angular-commands\/","title":{"rendered":"Angular Commands"},"content":{"rendered":"\n<p>Generate components in angular, usefull commands<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g component &lt;name&gt;\nng g c<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g directive &lt;name&gt;\nng g d<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g guard &lt;name&gt;\nng g g<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g interceptor &lt;name&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g module &lt;name&gt;\nng g m<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g pipe &lt;name&gt;\nng g p<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g service &lt;name&gt;\nng g s<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">List of some commonly used Angular pipes<\/h2>\n\n\n\n<p>Angular provides a variety of built-in pipes that you can use to transform and format data in your templates. Here&#8217;s a list of some commonly used Angular pipes, along with examples of how to use them:<\/p>\n\n\n\n<ol>\n<li><strong>DatePipe:<\/strong> Formats a date object into a string representation based on the provided format.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying the current date in a specific format --&gt;\n   {{ today | date: 'dd\/MM\/yyyy' }}<\/code><\/pre>\n\n\n\n<ol start=\"2\">\n<li><strong>UpperCasePipe:<\/strong> Converts a string to all uppercase letters.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Converting a string to uppercase --&gt;\n   {{ 'Hello, Angular' | uppercase }}<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li><strong>LowerCasePipe:<\/strong> Converts a string to all lowercase letters.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Converting a string to lowercase --&gt;\n   {{ 'Hello, Angular' | lowercase }}<\/code><\/pre>\n\n\n\n<ol start=\"4\">\n<li><strong>CurrencyPipe:<\/strong> Formats a number as currency, with options for specifying the currency code and formatting.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying a number as currency --&gt;\n   {{ price | currency: 'USD':true }}<\/code><\/pre>\n\n\n\n<ol start=\"5\">\n<li><strong>DecimalPipe:<\/strong> Formats a number as a decimal with options for specifying the number of digits and decimal places.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying a number with 2 decimal places --&gt;\n   {{ value | number: '1.2-2' }}<\/code><\/pre>\n\n\n\n<ol start=\"6\">\n<li><strong>PercentPipe:<\/strong> Formats a number as a percentage.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying a number as a percentage --&gt;\n   {{ fraction | percent }}<\/code><\/pre>\n\n\n\n<ol start=\"7\">\n<li><strong>JsonPipe:<\/strong> Converts a JavaScript object into a JSON string.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying a JSON object as a string --&gt;\n   {{ data | json }}<\/code><\/pre>\n\n\n\n<ol start=\"8\">\n<li><strong>AsyncPipe:<\/strong> Handles observables and promises and automatically subscribes\/unsubscribes in the template.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying the value of an observable --&gt;\n   {{ observableData | async }}<\/code><\/pre>\n\n\n\n<ol start=\"9\">\n<li><strong>SlicePipe:<\/strong> Extracts a portion of an array or string.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;!-- Example: Displaying the first 3 items of an array --&gt;\n   {{ arrayData | slice:0:3 }}<\/code><\/pre>\n\n\n\n<ol start=\"10\">\n<li><strong>TitleCasePipe:<\/strong> Converts a string to title case (the first letter of each word is capitalized). <br><code>&lt;!-- Example: Converting a string to title case --> {{ 'hello world' | titlecase }}<\/code><br><\/li>\n\n\n\n<li><strong>I18nPluralPipe:<\/strong> Selects an output string according to the pluralization rules for a given locale. <br><br><code>&lt;!-- Example: Selecting a pluralized string based on a count --> {{ itemCount | i18nPlural: { '=0': 'No items', '=1': 'One item', 'other': '# items' } }}<\/code><br><\/li>\n\n\n\n<li><strong>I18nSelectPipe:<\/strong> Selects an output string based on a mapping of input values to output strings for a given locale. <br><code>&lt;!-- Example: Selecting an output string based on an input value --> {{ status | i18nSelect: { 'in-progress': 'Work in progress', 'completed': 'Task completed', 'pending': 'Task pending' } }}<\/code><\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p>Custom pipes can also be created if specific data transformation needs that are not covered by the built-in pipes. <\/p>\n\n\n\n<p>Custom pipes allows to encapsulate and reuse data formatting logic in your Angular application.<\/p>\n\n\n\n<p>List of Angular directives<\/p>\n\n\n\n<p>Angular provides a variety of built-in directives that you can use to extend HTML and add dynamic behavior to your templates. Here&#8217;s a list of some commonly used Angular directives with examples:<\/p>\n\n\n\n<ol>\n<li><strong>ngIf:<\/strong> Conditionally renders an element based on a boolean expression.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;div *ngIf=\"isUserLoggedIn\"&gt;Welcome, User!&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<ol start=\"2\">\n<li><strong>ngFor:<\/strong> Iterates over a collection and generates HTML for each item.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;ul&gt;\n     &lt;li *ngFor=\"let item of items\"&gt;{{ item }}&lt;\/li&gt;\n   &lt;\/ul&gt;<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li><strong>ngSwitch:<\/strong> Conditionally renders content based on matching cases.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;div &#91;ngSwitch]=\"userRole\"&gt;\n     &lt;div *ngSwitchCase=\"'admin'\"&gt;Admin Dashboard&lt;\/div&gt;\n     &lt;div *ngSwitchCase=\"'user'\"&gt;User Dashboard&lt;\/div&gt;\n     &lt;div *ngSwitchDefault&gt;Guest Dashboard&lt;\/div&gt;\n   &lt;\/div&gt;<\/code><\/pre>\n\n\n\n<ol start=\"4\">\n<li><strong>ngClass:<\/strong> Adds or removes CSS classes based on conditions.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;div &#91;ngClass]=\"{ 'active': isActive, 'disabled': isDisabled }\"&gt;Clickable Element&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<ol start=\"5\">\n<li><strong>ngStyle:<\/strong> Sets inline styles based on expressions.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;div &#91;ngStyle]=\"{ 'font-size.px': fontSize, 'color': fontColor }\"&gt;Styled Text&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<ol start=\"6\">\n<li><strong>ngModel:<\/strong> Two-way data binding for form elements.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;input &#91;(ngModel)]=\"userName\" placeholder=\"Enter your name\"&gt;<\/code><\/pre>\n\n\n\n<ol start=\"7\">\n<li><strong>ngIfElse:<\/strong> Provides an alternative template when ngIf condition is false.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;div *ngIf=\"isUserLoggedIn; else notLoggedIn\"&gt;Welcome, User!&lt;\/div&gt;\n   &lt;ng-template #notLoggedIn&gt;Please log in to access the content.&lt;\/ng-template&gt;<\/code><\/pre>\n\n\n\n<ol start=\"8\">\n<li><strong>ngTemplateOutlet:<\/strong> Renders the content of a named template.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;ng-container *ngTemplateOutlet=\"myTemplate\"&gt;&lt;\/ng-container&gt;\n   &lt;ng-template #myTemplate&gt;&lt;p&gt;This is a custom template.&lt;\/p&gt;&lt;\/ng-template&gt;<\/code><\/pre>\n\n\n\n<ol start=\"9\">\n<li><strong>ng-container:<\/strong> A structural directive used for grouping elements without introducing an extra HTML element.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;ng-container *ngFor=\"let item of items\"&gt;\n     &lt;div&gt;{{ item }}&lt;\/div&gt;\n   &lt;\/ng-container&gt;<\/code><\/pre>\n\n\n\n<ol start=\"10\">\n<li><strong>ngDisable:<\/strong> Disables or enables an element based on a condition. <\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button &#91;disabled]=\"isButtonDisabled\">Click me&lt;\/button><\/code><\/pre>\n\n\n\n<ol start=\"10\">\n<li><strong>ngSubmit:<\/strong> Used in forms to handle form submission. <\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form (ngSubmit)=\"onFormSubmit()\"> &lt;!-- Form inputs go here --> &lt;button type=\"submit\">Submit&lt;\/button> &lt;\/form><\/code><\/pre>\n\n\n\n<p>Angular also allows to create custom directives to extend HTML behavior.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generate components in angular, usefull commands List of some commonly used Angular pipes Angular provides a variety of built-in pipes that you can use to transform and format data in your templates. Here&#8217;s a list of some commonly used Angular pipes, along with examples of how to use them: Custom pipes can also be created [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":458,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,101],"tags":[102,106,103,104,105],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/570"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":9,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":585,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions\/585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/458"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}