Rendy Rendy - 4 months ago 10
AngularJS Question

Accessing constant in template in AngularJS?

I have following code:

Javascript

var app = angular.module('myApp');

const TESTCONST = 'test 1';

app.constant("TEST", {
"TEST1": "test 1"
});


pug

both of this

div(ng-click="changeMessageType(TESTCONST)") Test


and this, don't work:

div(ng-click="changeMessageType(TEST.TEST1)") Test


it will give me undefined.

What is the correct way to do it?

Answer

What is the correct way to do it?

Correct way is to set scope/controller properties with whatever you need to be available in template. So you need to inject your constant in controller and do something like this:

.controller('Controller', function($scope, TEST) {
  $scope.TEST = TEST
})
Comments