Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js: Is .value() the proper way to set app wide constant and how to retrieve it in a controller

Hi there I was watching a couple of the angular.js videos and saw that the value() method was used to set a kind of module-wide constant. for example, one can set the Angular-UI library's config like so: (coffeescript)

angular.module('app',[]) .value "ui.config",    tinymce:     theme: 'simple'     width: '500'     height: '300' 

And my app is currently looking like this:

window.app = angular.module("app", [ 'ui'])  .config(["$routeProvider", ($routeProvider) ->   $routeProvider   .when "/users",     templateUrl: "assets/templates/users/index.html"     controller: IndexUsersCtrl    .otherwise redirectTo: "/users"  ])  .value 'csrf', $('meta[name="csrf-token"]').attr('content') #<---- attention here  IndexUsersCtrl = ($scope) ->   $scope.users = gon.rabl   console.log "I want to log the csrf value here" #<---- then attention IndexUsersCtrl.$inject = ['$scope'] 

But I can't seem to get that value by tapping into the 'app' variable which is corresponding to the app module.

I read up here on ST and over on angularjs's google group that one way to share common code btwn controllers is through a service, will this concept apply here, too?

Thanks!

like image 479
Nik So Avatar asked Oct 22 '12 16:10

Nik So


People also ask

What is difference between config () and run () method in AngularJS?

config block is executed during the provider registration and configuration phase. It' a module level block. The . run block is executed after the config block.

What is constant in AngularJS?

Constant are like services in AngularJS in which we can define our globally data. It is declare using "constant" keyword. As we define our app-keys in Web.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.

What is the use of app JS in Angular?

js is a JavaScript file in which I created an instance of an Angular module. The controller. js Javascript file has an Angular controller that is registered with the app. js Angular module and contains business logic (programming between end UI and database).


1 Answers

Module.value(key, value) is used to inject an editable value, Module.constant(key, value) is used to inject a constant value

The difference between the two isn't so much that you "can't edit a constant", it's more that you can't intercept a constant with $provide and inject something else.

// define a value app.value('myThing', 'weee');  // define a constant app.constant('myConst', 'blah');  // use it in a service app.factory('myService', ['myThing', 'myConst', function(myThing, myConst){    return {        whatsMyThing: function() {            return myThing; //weee        },        getMyConst: function () {           return myConst; //blah        }    }; }]);  // use it in a controller app.controller('someController', ['$scope', 'myThing', 'myConst',      function($scope, myThing, myConst) {         $scope.foo = myThing; //weee         $scope.bar = myConst; //blah     }); 
like image 137
Ben Lesh Avatar answered Sep 28 '22 04:09

Ben Lesh