Shiva Goud A Shiva Goud A - 9 months ago 26
Javascript Question

Unknown ENV provider in AngularJS

I writing one constant file with Reff,
I getting the follwing Exception If I tried like that :


angular.min.js:118 Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=ENVProvider%20%3C-%20ENV%20%3C-%20viewRoleController



I get the error above in console:
My code as follows,

jsp:- included constant file location

angularController

var app = angular.module('App', ['angularUtils.directives.dirPagination','ui.bootstrap']);

app.controller('viewRoleController', function($scope, $http, ENV, viewRoleService) {

var dynamicData;

$http.get('js/commons/AngularConstantFile.js').then(function (response) {
dynamicData = response.data;
});
});


Constant file:-

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

app.constant("ENV", {
"status412": "412",
"Success": "Success",
});

lin lin
Answer Source

Try it like in this simple demo fiddle. Ensure your config is loaded after your main application into DOM and you will be fine.

AngularJS application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope, ENV) {
    $scope.ENV = ENV;
})

myApp.constant("ENV", {
  "status412": "412",
  "Success": "Success",
});

An other approach is do create your config in an own module and parse it into your main application like in this demo fiddle. This will also work if your config is outsourced into an own file.

AnuglarJS application

var config = angular.module('config',[]).constant("ENV", {
  "status412": "412",
  "Success": "Success",
});

var myApp = angular.module('myApp', ['config']);

myApp.controller('MyCtrl', function ($scope, ENV) {
    $scope.ENV = ENV;
});