Skip to content

Instantly share code, notes, and snippets.

@m00s
Last active August 29, 2015 14:20
Show Gist options
  • Save m00s/cd816a47455e4b591274 to your computer and use it in GitHub Desktop.
Save m00s/cd816a47455e4b591274 to your computer and use it in GitHub Desktop.
AngularJS constants uses

I use constants both in controllers and in config blocks, so give it a go:

  1. git clone https://gist.github.com/cd816a47455e4b591274.git angular-constants
  2. cd angular-constants
  3. make
  4. open constants.html

and let me know what you think..

<!doctype html>
<html>
<head>
<script src="bundle.js"></script>
</head>
<body>
<h1> Constants examples </h1>
<div ng-controller="constantsCtrl as constants">
<p> simpleConstant: <b> {{ constants.simpleConstant }} </b></p>
<p> paramConstant: <b> {{ constants.paramConstant }} </b></p>
<p> IIFEConstant: <b> {{ constants.IIFEConstant }} </b></p>
<p> moreComplexConstant: <b> {{ constants.moreComplexConstant }} </b></p>
<p> moreComplexConstantAfterSetterCall: <b> {{ constants.moreComplexConstantAfterSetterCall }} </b></p>
</div>
</body>
</html>
'use strict';
var angular = require('angular');
angular.module('app.constants', [])
.constant('simpleConstant', 'constant')
.constant('paramConstant', function(value){
return 'constant ' + value;
})
.constant('IIFEConstant', (function(){
return 'constant as return value';
})())
.constant('moreComplexConstant', (function(){
var _text = 'constant (default)';
return {
text: _text,
setText: function (value) {
this.text = value;
}
}
})())
.config(function(simpleConstant, paramConstant, IIFEConstant, moreComplexConstant) {
console.log('simpleConstant:', simpleConstant);
console.log('paramConstant:', paramConstant('with param'));
console.log('IIFEConstant:', IIFEConstant);
console.log('moreComplexConstant.text:', moreComplexConstant.text);
moreComplexConstant.setText('a new constant value');
console.log('moreComplexConstant.text:', moreComplexConstant.text);
})
.controller('constantsCtrl', function(simpleConstant, paramConstant, IIFEConstant, moreComplexConstant) {
var vm = this;
vm.simpleConstant = simpleConstant;
vm.paramConstant = paramConstant('with param');
vm.IIFEConstant = IIFEConstant;
vm.moreComplexConstant = moreComplexConstant.text;
moreComplexConstant.setText('a new constant value');
vm.moreComplexConstantAfterSetterCall = moreComplexConstant.text;
});
angular.element(document).ready(function() {
angular.bootstrap(document, ['app.constants']);
});
# Main entry point
SOURCE = constants.js
# The dependencies (other than node_modules/**)
LIBS = $(shell ls package.json)
# The target
TARGET = bundle.js
# Binaries
BROWSERIFY = ./node_modules/.bin/browserify
NPM = npm
.PHONY: build clean watch
build: $(TARGET)
clean:
rm -f $(TARGET)
# Note: browserify --list is slow, just rely on node_modules
$(TARGET): $(SOURCE) node_modules
$(BROWSERIFY) -o $@ -- $<
node_modules:
$(NPM) install
{
"private": true,
"author": "Massimiliano Sartoretto",
"license": "MIT",
"scripts": {
"build": "browserify constants.js -o bundle.js"
},
"dependencies": {
"angular": "1.4.x",
"jquery": "2.1.3"
},
"browser": {
"angular": "./node_modules/angular/angular.js"
},
"browserify-shim": {
"angular": {
"depends": "jquery:jQuery",
"exports": "angular"
}
},
"browserify": {
"transform": [
"browserify-shim"
]
},
"devDependencies": {
"browserify": "9.0.x",
"browserify-shim": "3.8.x"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment