{"id":290,"date":"2018-06-25T13:11:31","date_gmt":"2018-06-25T13:11:31","guid":{"rendered":"http:\/\/www.bullcrane.com\/angular\/?p=290"},"modified":"2018-06-30T15:02:02","modified_gmt":"2018-06-30T15:02:02","slug":"global-constants","status":"publish","type":"post","link":"http:\/\/www.bullcrane.com\/angular\/2018\/06\/25\/global-constants\/","title":{"rendered":"Global constants"},"content":{"rendered":"<p>One thing that was bugging me in my <a href=\"http:\/\/www.bullcrane.com\/angular\/2018\/05\/07\/flex-layout-performance-solution\/\">flex-layout performance solution<\/a> was how I defined the global constants for my three layouts PHONE, TABLET, and HD.  As always with constants, I&#8217;m just trying to make the code clearer to the reader.  There is nothing more to this than PHONE is 1, TABLET is 2, and HD is 3.<\/p>\n<p>We are not able to use the <em>const<\/em> declaration inside of class definitions.<\/p>\n<pre>const sizeNum = {PHONE:1,TABLET:2,HD:3}<\/pre>\n<p>Attempting this results in:<\/p>\n<pre> error TS1248: A class member cannot have the 'const' keyword.<\/pre>\n<p>We could define the constants outside of the class by putting the line of code just above the class, but this isn&#8217;t good encapsulation, and code fragments in the html template cannot see the constants.  With the constants defined outside of the class, something like this is impossible:<\/p>\n<pre>&lt;div *ngIf=\"activeSize()&gt;sizeNum.PHONE\"&gt;<\/pre>\n<p>Because of these issues, I resorted to a plain old hardcoded object, defined as usual inside of the class:<\/p>\n<pre>sizeNum: Object = {PHONE:1,TABLET:2,HD:3}<\/pre>\n<p>This doesn&#8217;t set up true constants.  It is possible to programmatically change the sizeNum object later (eg. sizeNum[&#8216;PHONE&#8217;]=5).  Also, they are not global, and so I wound up having to pass the sizeNum object as a parameter to other components.  Awkward.<\/p>\n<p>There is a better way.<\/p>\n<h6>Angular dependancy injection<\/h6>\n<p>I am accustomed to using dependancy injection for full fledged classes, but it also can inject simpler objects.  This works nicely for global constants.  Set them up in app.module.ts:<\/p>\n<p>app.module.ts<\/p>\n<pre>import { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgModule } from '@angular\/core';\r\nimport { PlaylstService } from '.\/playlst.service';\r\n...\r\n<span style=\"color:red\">const sizeNum = {PHONE:1,TABLET:2,HD:3}<\/span>\r\n@NgModule({\r\n  declarations: [\r\n    AppComponent\r\n    ],\r\n  imports: [\r\n    BrowserModule,\r\n    BrowserAnimationsModule,\r\n...\r\n    ],\r\n  providers: [\r\n    PlaylstService,\r\n    <span style=\"color:red\">{ provide:'SizeConstants',useValue:sizeNum }<\/span>\r\n    ],\r\n  bootstrap: [AppComponent]\r\n  })\r\nexport class AppModule { }\r\n<\/pre>\n<p>Put them into the constructor in desired components:<\/p>\n<pre>import { Component,\r\n         <span style=\"color:red\">Inject<\/span> } from '@angular\/core';\r\nimport { HttpClient,\r\n         HttpHeaders } from '@angular\/common\/http';\r\n...\r\nimport { PlaylstService } from '.\/playlst.service';\r\n\r\n@Component({\r\n  selector: 'app-root',\r\n  templateUrl: '.\/app.component.html',\r\n  styleUrls: ['.\/app.component.css']\r\n  })\r\n\r\nexport class AppComponent {\r\n\r\n  accessToken: string;      \/\/ access token for the session.  we grep this outta the url\r\n...\r\n  constructor (\r\n    private http: HttpClient,\r\n    private plistSvc: PlaylstService,\r\n    <span style=\"color:red\">@Inject('SizeConstants') public szNum: any<\/span>\r\n    ) {\r\n    \/\/ initializations\r\n    try {this.accessToken=window.location.hash.match(\/^#access_token=([^&]+)\/)[1];}\r\n    catch (ERR) {this.accessToken='BAD';}\r\n...\r\n<\/pre>\n<p>Make use of them, no problem even in html code fragments:<\/p>\n<pre>&lt;div *ngIf=\"activeSize()&gt;szNum.PHONE\"&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One thing that was bugging me in my flex-layout performance solution was how I defined the global constants for my three layouts PHONE, TABLET, and HD. As always with constants, I&#8217;m just trying to make the code clearer to the reader. There is nothing more to this than PHONE is 1, TABLET is 2, and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.bullcrane.com\/angular\/2018\/06\/25\/global-constants\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Global constants&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts\/290"}],"collection":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/comments?post=290"}],"version-history":[{"count":14,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":307,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts\/290\/revisions\/307"}],"wp:attachment":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}