Rendy Rendy - 1 year ago 81
AngularJS Question

Accessing constant in template in AngularJS?

I have following code:


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

const TESTCONST = 'test 1';

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


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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download