Javascript 在函数外部添加自定义过滤器时,未定义Angular app
尝试按照一些示例进行操作,但我发现应用程序未定义 app.jsJavascript 在函数外部添加自定义过滤器时,未定义Angular app,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,尝试按照一些示例进行操作,但我发现应用程序未定义 app.js (function () { "use strict"; var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']); }()); 所以我希望能够使用或附加到“应用程序” 我有一个控制器js文件 (function () { "use strict"; angular .module("dev
(function () {
"use strict";
var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']);
}());
所以我希望能够使用或附加到“应用程序”
我有一个控制器js文件
(function () {
"use strict";
angular
.module("deviceManagement")
.controller("DeviceListCtrl",
ProductListCtrl);
function ProductListCtrl($http, $scope) {
var vm = this;
vm.devices = [];
deviceList();
function deviceList() {
//..........
}
}
} ());
那么就在上面的代码下面我做这个
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文件 此外,我将更改以下内容:
app.filter('deviceStatus', function () {
为此:
angular
.module("deviceManagement")
.filter('deviceStatus', function () {
最好不要使用var应用程序,只参考模块,例如angular.module(“设备管理”)。看看这个。在你的情况下,问题是这里:
在lambda函数中有
app
,它就存在于其中
要使其发挥作用,您应该具备:
因此,这里我们在lambda中注入window
元素,以便您可以全局定义app
(window.app=app |{}
)
注:考虑Windows应用程序的方法不是最好的。
可能
angular.module(“deviceManagement”)
语法是最好的方法,可以让代码更加模块化 我认为你应该在你的deviceListCtrl.js中使用app.controller来代替angular.controllerYep@ScottL。这绝对是最好的方法。谢谢,这对我来说现在有意义了。1。你不是说生活吗?(不是lambda)2。什么是更好的方法?谢谢@CodingAway我不得不承认我不知道(function(){…})(
的模式名是IIFE。语法function()
用于匿名函数,因此我称之为lambda
。@ScottL建议的最佳方法是angular.module(“deviceManagement”)
一个,但它没有解释您接收应用程序未定义的原因。
(function () {
"use strict";
var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']);
}());
(function (window) {
"use strict";
var app = angular.module("deviceManagement",['angularUtils.directives.dirPagination']);
window.app = app || {};
}(window));