2

Trying to follow some examples, but I get app is not defined

app.js

(function () {
   "use strict";
   var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']);
}());

So I have HOPING to be able to use or attach to "app"

I have a controller js file

(function () {
   "use strict";
angular
    .module("deviceManagement")
    .controller("DeviceListCtrl",
    ProductListCtrl);

function ProductListCtrl($http, $scope) {
    var vm = this;

    vm.devices = [];

    deviceList();


    function deviceList() {

      //..........
    }

  }
} ());

THEN RIGHT UNDER the ABOVE CODE I DO THIS

app.filter('deviceStatus', function () {

    var deviceStatusLookup = {
        1: "New Device",
        2: "Activated",
        3: "Unactivated"
    };

    return function (statusId) {
        var output = deviceStatusLookup[statusId];
        return output;
    }
});

Error on page console

deviceListCtrl.js:73 Uncaught ReferenceError: app is not defined
1
  • i think you should use app.controller in your deviceListCtrl.js in place of angular.controller Commented Sep 2, 2016 at 0:59

2 Answers 2

3

Check that you have included the app.js file.

Also, I would change the below:

app.filter('deviceStatus', function () {

to this:

angular
    .module("deviceManagement")
    .filter('deviceStatus', function () {

It is a good idea to not use var app and to just refer to the module, e.g. angular.module("deviceManagement"). See this answer.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep @ScottL this one is definitely the best approach.
Thanks , this make sense to me now.
0

Well in you case the issue is that in here:

(function () {
   "use strict";
   var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']);
}());

You have app in a lambda function, and it exists just in there.

To make it works you should have:

(function (window) {
   "use strict";
   var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']);
   window.app = app || {};
}(window));

So here we are injecting the window element in the lambda so you can define app globally (window.app = app || {}).

Note: Consider that the window.app approach is not the best.

Probably the angular.module("deviceManagement") syntax is the best way to go, in a way to have your code more modular.

3 Comments

1. don't you mean IIFE? (not lambda) 2. What is a better approach?
thanks @CodingAway i have to confess you that i didn't know that the pattern name of the (function(){ ... })() was IIFE. The syntax function() is for anonymous functions and so I called lambda
The best approach as @ScottL suggested is the angular.module("deviceManagement") one but it was not explaining why you were receiving app is not defined .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.