1

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",
});
Community
  • 1
  • 1
Shiva Goud A
  • 322
  • 1
  • 6
  • 18
  • You are the non accept answer boy. IMO, you should get no help. Still waiting for feedback here: http://stackoverflow.com/questions/41978567/how-to-hide-a-link-by-using-ids also all other users on your questions still waiting for your feedback. – lin Mar 12 '17 at 11:46
  • @lin thanks for your help, If my problem got solved then I will sure accept answer otherwise How Can I. – Shiva Goud A Mar 12 '17 at 11:53
  • This is a poor lie m8. The most of your questions solved by good answer but you did not even give a feedback on it. Also my answers here http://stackoverflow.com/questions/41978567/how-to-hide-a-link-by-using-ids does solve your problem (where I spend a lot of time to give you a full qualified answer). You never called back, even if I asked you for. Sorry, but this is not how stackoverflow works. – lin Mar 12 '17 at 11:56
  • @lin I will respect your sincerity on your kind helping nature. But really those answers are not satisfied my requirement. I dont thinks so It is your's problem. I think It is my understand problem. my apologies for that.I learnt new lession from here. – Shiva Goud A Mar 12 '17 at 12:04
  • Give feedback on your other questions and mark right answers. You recived a lot of good answers on your questions. If a user writes a comment like `Are you using angularjs ? if so provide the code of your controller` - you should may going to answer this question, else how should the user be able to give you a right answer. If an other user ask you for feedback on his answer, you should give it to give the user a possibility to solve your problem. But the worst you can do is "nothing", like you did it on all your questions right now. – lin Mar 12 '17 at 12:08
  • Take care of your other questions, bring some feedback and I will help you here. – lin Mar 12 '17 at 12:10

2 Answers2

1

Just change your constant js file as follows, which will make sure to use the global instance of the module

angular.module('App').constant("ENV", {
  "status412": "412",
  "Success": "Success",
});
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Hey Sajee, isnt it the same like getting the global module like `var app = angular.module('App');` and than procced with `app.constant...`? – lin Mar 12 '17 at 12:52
  • Yep its the same, this doesnt solve the problem: https://plnkr.co/edit/7lGKHPMlUkL72RucWzlo?p=preview – lin Mar 12 '17 at 13:00
0

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;
});
lin
  • 17,956
  • 4
  • 59
  • 83