在函数之外添加自定义过滤器时未定义 Angular 应用程序 [英] Angular app is not defined when adding a custom filter outside of function

查看:23
本文介绍了在函数之外添加自定义过滤器时未定义 Angular 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试遵循一些示例,但我得到 未定义应用

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"

我有一个控制器js文件

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;
    }
});

页面控制台错误

deviceListCtrl.js:73 Uncaught ReferenceError: app is not defined

推荐答案

检查您是否包含了 app.js 文件.

Check that you have included the app.js file.

另外,我会更改以下内容:

Also, I would change the below:

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

到此:

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

最好不要使用 var app 并只引用模块,例如angular.module(设备管理").请参阅此答案.

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.

这篇关于在函数之外添加自定义过滤器时未定义 Angular 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆