{"id":3487,"date":"2019-06-02T17:57:57","date_gmt":"2019-06-02T17:57:57","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3487"},"modified":"2021-06-15T15:12:19","modified_gmt":"2021-06-15T15:12:19","slug":"code-snippets","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/code-snippets\/","title":{"rendered":"Directives Basics"},"content":{"rendered":"<p><strong class=\"ctaHeader3\">Code Snippets &#8211; Table of Contents<\/strong><\/p>\n<ol>\n<li>\n   Builtin Angular Directives<\/p>\n<ol>\n<li><a href=\"#ADNCS\">Attribute Directive &#8211; NgStyle, NgClass<\/a><\/li>\n<li><a href=\"#SDNIF\">Structural Directive &#8211; *NgIf, *NgFor, *NgSwitch<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\"><a name=\"ADNCS\">Attribute Directive &#8211; ngClass and ngStyle<\/a><\/strong><br \/>\n<strong>ngClass<\/strong><\/p>\n<ol>\n<li>For using ngClass the styling should be in JSON Obect<\/li>\n<li>Advantage of using ngClass is applying more than one class to the DOM Element unlike class<\/li>\n<li>In the below code you see class which applies present or absent based on one true or false returned by getResult function<\/li>\n<li>In the registration.component.ts we have declared a JSON object which applies 2 classes present and normal or absent and bold based on true or false returned by getStatus(method)<\/li>\n<\/ol>\n<p><strong>Styling using class<\/strong><br \/>\n<em>registration.component.html<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;td &#x5B;class]=&quot;getStatus()?'present':'absent'&quot;&gt;{{objStudent.science}}&lt;\/td&gt;\r\n.\r\n.\r\n.\r\n<\/pre>\n<p><strong>Styling using ngClass<\/strong><br \/>\n<em>registration.component.html<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;td &#x5B;ngclass]=&quot;cssStyle&quot;&gt;{{objStudent.science}}&lt;\/td&gt;\r\n.\r\n.\r\n.\r\n\r\n<\/pre>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nexport class RegistrationComponent implements OnInit { \r\n  public cssStyle;  \r\n \r\n  ngOnInit() {\r\n     this.cssStyle = {\r\n       &quot;present&quot; : getStatus(),\r\n       &quot;absent&quot;  : !getStatus(),\r\n       &quot;normal&quot;  : getStatus()\r\n       &quot;bold&quot;    : !getStatus(),       \r\n     };\r\n  }\r\n}\r\n\r\n.\r\n.\r\n.\r\n\r\n<\/pre>\n<p><\/p>\n<p><strong>ngStyle<\/strong><\/p>\n<ol>\n<li>ngStyle is to apply style directly instead of defining style using class<\/li>\n<li>Same registration.component.ts has been modified to take style attributed such as color and weight<\/li>\n<li><\/li>\n<\/ol>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nexport class RegistrationComponent implements OnInit { \r\n  public cssStyle;  \r\n \r\n  ngOnInit() {\r\n     this.cssStyle = {\r\n       &quot;background-color&quot;  : getStatus()?&quot;green&quot;:&quot;red&quot;,\r\n       &quot;font-weight&quot;       : getStatus()?&quot;normal&quot;:&quot;bold&quot;       \r\n     };\r\n  }\r\n}\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>There is an alternative for both above [class] and [ngClass] as below<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;td &#x5B;class.positive]=&quot;getStatus()&quot; &#x5B;class.negative]=&quot;!getStatus()&quot;&gt;{{objStudent.science}}&lt;\/td&gt;.\r\n.\r\n<\/pre>\n<p>Similarly for [ngStyle] as below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;td &#x5B;style.background-color]=&quot;getStatus()?'green':'red'&quot;&gt;{{objStudent.science}}&lt;\/td&gt;.\r\n.\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"SDNIF\">Structural Directive &#8211; ngIf, ngFor and ngSwitch<\/a><\/strong><br \/>\n<strong>ngIf<\/strong><\/p>\n<ol>\n<li>In the below code we decide the words to be displayed based on the value returned by getStatus(objStudent) method<\/li>\n<li>Pass would be displayed if true or else fail would be returned<\/li>\n<\/ol>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;span *ngIf=&quot;getStatus(objStudent)&quot;&gt;Pass&lt;\/span&gt;\r\n&lt;span *ngIf=&quot;!getStatus(objStudent)&quot;&gt;Fail&lt;\/span&gt;\r\n.\r\n<\/pre>\n<p><strong>ngFor<\/strong><\/p>\n<ol>\n<li>In the below code we use *ngFor loop to iterate over array of student object<\/li>\n<li>We are generating serial no for first column by adding 1 to variable i and displaying name of student in second column<\/li>\n<li>*ngFor has other loacl values apart from indes such as even, odd, first, last based on which manipulations could be carried out <\/li>\n<\/ol>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nexport class RegistrationComponent implements OnInit {\r\n  public arrStudent:Array&lt;Student&gt;;\r\n\r\n  ngOnInit() {\r\n     this.arrStudent = &#x5B;new Student(101, 'Mugil', 31, 74, 65,55,64,84),\r\n                        new Student(102, 'Manasa', 26, 31, 65,55,86,84),\r\n                        new Student(103, 'Kavitha', 27, 90, 65,60,84,46),\r\n                        new Student(104, 'Renu', 28, 31, 65,55,84,46),\r\n                        new Student(105, 'Joseph', 23, 31, 65,55,89,84)];\r\n\r\n  }\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;tr&gt;\r\n    &lt;th&gt;Sno&lt;\/th&gt;\r\n    &lt;th&gt;Student ID&lt;\/th&gt;\r\n&lt;\/tr&gt;\r\n&lt;tbody&gt;\r\n  &lt;tr *ngFor=&quot;let objStudent of arrStudent; index as i;&quot;&gt;\r\n     &lt;td&gt;{{i+1}}&lt;\/td&gt;\r\n     &lt;td&gt;{{objStudent.studentId}}&lt;\/td&gt;\r\n     .\r\n     .\r\n     .\r\n     .\r\n  &lt;\/tr&gt;\r\n&lt;\/tbody&gt;\r\n.\r\n.\r\n<\/pre>\n<p>Now let&#8217;s see a simple example of applying a different color to text based on odd and even rows<\/p>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;tr *ngFor=&quot;let objStudent of arrStudent; index as i;odd as isOdd; even as isEven&quot; &#x5B;class]=&quot;isOdd?'odd':'even'&quot;&gt;\r\n     .\r\n     &lt;td&gt;{{i+1}}&lt;\/td&gt;\r\n     &lt;td&gt;{{objStudent.studentId}}&lt;\/td&gt;\r\n     .\r\n&lt;\/tr&gt;\r\n<\/pre>\n<p>the same code can be written as below where the only difference is styles are applied seperately evaluating for both odd and even.<br \/>\n<strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;tr *ngFor=&quot;let objStudent of arrStudent; index as i;odd as isOdd; even as isEven&quot; &#x5B;class]=&quot;isOdd&quot; &#x5B;class.even]=&quot;isEven&quot;&gt;\r\n     .\r\n     &lt;td&gt;{{i+1}}&lt;\/td&gt;\r\n     &lt;td&gt;{{objStudent.studentId}}&lt;\/td&gt;\r\n     .\r\n&lt;\/tr&gt;\r\n<\/pre>\n<p>We can define the template directly in a component instead of giving the templateURL using a template and define styleURL instead of styles.<\/p>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n@Component({\r\n  selector: 'app-registration',\r\n  template: '&lt;tr&gt;\r\n              &lt;td&gt;{{objStudent.studentId}}&lt;\/td&gt;\r\n              &lt;td&gt;{{objStudent.studentName}}&lt;\/td&gt;\r\n            &lt;\/tr&gt;',\r\n  styleUrls: &#x5B;'.\/registration.component.css']\r\n})\r\n.\r\n.\r\n.\r\n<\/pre>\n<p><strong>registration.component.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n@Component({\r\n  selector    : 'app-registration',\r\n  templateURL : 'app-registration.html',\r\n  styles      : &#x5B;'\r\n                   .present\r\n                   {\r\n                      background-color: greenyellow;\r\n                   }\r\n\r\n                   .absent\r\n                   {\r\n                      background-color: #fc8674;\r\n                   }\r\n                ']\r\n})\r\n.\r\n.\r\n.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Code Snippets &#8211; Table of Contents Builtin Angular Directives Attribute Directive &#8211; NgStyle, NgClass Structural Directive &#8211; *NgIf, *NgFor, *NgSwitch Attribute Directive &#8211; ngClass and ngStyle ngClass For using ngClass the styling should be in JSON Obect Advantage of using ngClass is applying more than one class to the DOM Element unlike class In the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/code-snippets\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300],"tags":[],"class_list":["post-3487","post","type-post","status-publish","format-standard","hentry","category-directives"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3487"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3487\/revisions"}],"predecessor-version":[{"id":4352,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3487\/revisions\/4352"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}