Coding Away Coding Away - 3 months ago 11
Javascript Question

Angular app is not defined when adding a custom filter outside of function

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

Answer

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.

Comments