angular


How to make a string constant in angularjs 4?


In my service, I am using a http post. I want to set the url as a constant.
return this.http.get(this.config.API_URL+'users', options)
.map(res=>res.json());
I tried with a service:
import {Injectable} from '#angular/core';
#Injectable()
export class AppService {
API_URL :String;
constructor() {
this.API_URL = 'some url';
}
}
Is there any other method to make a constant value in Angular4 ?
I'm not sure if i understand your question but if you want to create constants, you can do it in a different class and import it.
constants.ts
export class Constants {
public static get HOME_URL(): string { return "sample/url/"; };
}
sample.component.ts
import { Constants } from "./constants";
#Component({
})
export class SampleComponent {
constructor() {
let url = Constants.HOME_URL;
}
}
You can simply export a constant using es6/typescript modules if that's all you need:
constants.ts:
export const API_URL: string = 'api/url';
And import where needed:
import { API_URL } from './constants.ts';
...
return this.http.get(API_URL+'users', options)
.map(res=>res.json());
or if you have many constants you can import them all:
import * as constants from './constants.ts';
...
return this.http.get(constants.API_URL+'users', options)
.map(res=>res.json());
If you want to make your constants configurable per application on startup, you can use providers. Check out the top answer in this link: how do I get angular2 dependency injection to work with value providers

Related Links

Find record through HorizonIO in RethinkDB not returning result
How to display json object parameters in angular 2 template typescript
ZoneAwareError when running agular2 application in safari
I want to bind the legend option with checkbox(show legend or not)
How to get value from asynchronous local storage function and pass it to modal ionic 2
Syntax highlighting in Angular 2
angular2-cli cannot load style error
Angular2/Rxjs: 'catch' on observable giving ProgressEvent instead of json from response
Display items depending on date issued
Opening menu using position, angular2 material
Undefined is not an object(evaluating) - angular
Font Awesome with Ionic2
angular view is not updating after component value gets changed
how do I merge the values of the child components of angular2 into reactive form object?
Angular2 multiple components different imports
Update an array with new objects

Categories

HOME
angular
audio
rstudio
qt5
sitecore8
function
form-submit
netlogo
drools
wolfram-mathematica
wlan
android-gradle
strongswan
terraform
purescript
crc32
translate
webassembly
salt-stack
axon
video.js
firebreath
sensenet
j
twincat-ads
uidocumentinteraction
httpsession
derivative
cloudify
polymer-1.0
robots.txt
google-nativeclient
xfce
jsdata
google-surveys
gembox-document
indexof
povray
jqassistant
protobuf-c
appery.io
magnolia
nsdate
fixtures
moa
spring-cloud-sleuth
google-console-developer
binary-image
angular-chart
inputstreamreader
xml-rpc
bubble-sort
optional
rpy2
google-chrome-arc
code-signing-entitlements
android-runtime
launch-agent
helium
dbx
android-image
decompression
gm
strstream
devpartner
correspondence-analysis
website-deployment
test-and-target
anonymity
shapely
angular-component-router
ios9.3
integer-overflow
pdl
mpld3
steroids
spatialite
eofexception
unexpectendoffile
dcraw
fad
ilias
resharper-plugins
syntactic-sugar
ios-provisioning
sine-wave
rhodecode
apiaxle
ios6.1
slimv
windmill
server-side-scripting
file-association
array-formulas
mysql-5.0
ctime
cck
xla
google-notebook

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile