Why i am able to change angular constant?

Hitesh Kumar

I am playing with angular constants. I am observing that I am able to change the value of the constant. I am not able to get it. Why I am able to change the value. I am creating the constant like this:

var app = angular.module('app', []);
app.constant('Type', {
  PNG: 'png',
  GIF: 'gif'
});
app.constant('serialId', 'aRandomId');

Even if I create the constant using angular.value then also I am able to change it. To change the value of constant I am doing this in my controller:

app.controller('MainController', ['$scope', 'Type', 'serialId', '$timeout', function($scope, Type, serialId, $timeout) {
  $scope.data = {
    type: Type,
    serialId: serialId
  };
  $timeout(function() {
    Type.PNG = 'this is changed';
    serialId = 'new Serial Id';
    console.log(serialId);
  }, 1000);
}]);

By the defination of constant what I get is constant is somehting whose value does not change and it has a fixed value. MDN says that once you declare the constant you can not change it if constant is not an object. eg.

const x=10;
x=20;//will throw the error.
const obj={};
obj.a='ab'; //will not throw error.

But in case of angular constant when I change the value nothing happens. It does not even notify that the value is changed. And there documentation also does not talk about changing the value of constants. If we can change the value of angular constant like a plain javascript variable then why do they are called constants? Here's the fiddle to play

Jeroen

There's a difference between:

  • Value types (strings, booleans, etc); and
  • Reference types (references to objects, arrays, etc);

A variable can be of either type.

Constant variables are called "constants" because you cannot change their content: you cannot set a new value or reference, respectively.

Put differently, for reference types being constant means you cannot change that variable so that it'll reference something else. As you noted, you can change the contents of whatever object a reference variable points to.

If you want to make an object itself "constant", you can use Object.freeze:

var app = angular.module('app', [])
  .constant('Type', Object.freeze({ PNG: 'png', GIF: 'gif' }))
  .constant('SerialId', 'asdfgh12345')
  .controller('myController', ['$timeout', 'Type', 'SerialId', MyController]);
  
function MyController($timeout, Type, SerialId) {
  var vm = this;
  
  // This .data property nor its properties are constant or frozen...
  vm.data = {
    type: Type,
    serialId: SerialId
  };
  
  $timeout(function() {
    Type.PNG = 'this is changed in timeout';
    SerialId = 'changed serial id in timeout';
  }, 1000);
  
  $timeout(function() {
    var el = document.getElementById('x');
    var injector = angular.element(el).injector();
    vm.secondData = {
      type: injector.get('Type'),
      serialId: injector.get('SerialId')
    }
  }, 2000);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
<div ng-app="app" ng-controller="myController as vm" id="x">
  <pre>{{ vm | json }}</pre>
</div>

Note that Object.freeze does not do so recursively, you'd need a function/library for that.

Note also that I snuck in some comments about SerialId. First up, realize that there's three different things named "SerialId":

  1. An angular constant names "SerialId";
  2. A function argument named "SerialId";
  3. The third item (string) in the dependencies array, "SerialId";

When the controller constructor function is run, the function argument will be filled with the value from the constant. The function argument could've been named Foo as well, by the way. You should consider that argument to be a local non-constant variable with the same value as the constant has.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why I am able to change member of an object which is passed as constant reference?

Why am I able to change the array

Why I am able to modify using constant range based loop?

Why I am able to assign constant shared_ptr to non constant shared_ptr in C++?

Why am I not able to change UIButton text-color?

Why am I not able to change the value of the boolean variable in this method

Why am I not able to change the legend title for this sf object plotting?

Why am I not able to change the shell with the chsh command?

Why i am not able to change the dummy variable here in my code?

Am I able to change templateUrl in angular 2 dynamically?

I am able to modify a constant string

Why am I not able to add a JPanel to a JPanel?

Why am I able to open this file?

Why am I not able to remove outliers?

Why am I not able use round in an extension?

Why am I not able to use $this is an opencart helper?

Why am I not able to drop a column in Vertica

Why am I not able to install JSON gem?

Why am I not able to open a VS project

Why am I not able to use * with touch in path?

Why am i not able to login a user?

Why am I not able to clone an object?

Why i am not able to create the Unique pointer

Why am I not able to buy applications?

Why am I not able to add this route?

Why am I able to change the value of a tensor without the computation graph knowing about it in Pytorch with detach?

Why I am not able to change the state of my react component the second time and got no error

I am not able to run angular application

why i am not able to make the text area partially non editable in angular 5 and typescript?