AngularJS 错误 [$injector:modulerr]
AngularJS Error [$injector:modulerr]
很抱歉代码太长,但我的 AngularJS 模块有问题,该模块是根据 these lessons 创建的。我希望我的 angular 模块与 ASP.NET MVC 一起工作,但我什至不能 运行 它。
(function () {
var AAngScript = angular.module('AAngScript', ['ngRoute']);
AAngScript.controller('LoginController', ['$scope', '$routeParams', '$location', 'LoginFactory', function ($scope,
$routeParams, $location, LoginFactory) {
$scope.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
$scope.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}]);
AAngScript.controller('RegisterController', ['$scope', '$location', 'RegistrationFactory', function ($scope, $location, RegistrationFactory) {
$scope.registerForm = {
emailAdress: '',
password: '',
confirmPassword: '',
registrationFailure: false
};
$scope.register = function () {
var result = RegistrationFactory($scope.registerForm.emailAdress, $scope.registerForm.password, $scope.registerForm.confirmPassword);
result.then(function (result) {
if (result.success) {
$location.path('/routeOne');
} else {
$scope.registerForm.registrationFailure = true;
}
});
}
}]);
AAngScript.factory('AuthHttpResponseInterceptor', ['$q', '$location', function ($q, $location) {
return {
response: function (response) {
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
},
responseError: function (rejection) {
if (rejection.status === 401) {
console.log("Response Error 401", rejection);
$location.path('/login').search('retutnUrl', $location.path());
}
return $q.reject(rejection);
}
}
}]);
AAngScript.factory('LoginFactory', ['$http', '$q', function ($http, $q) {
return function (emailAdress, password, rememberMe) {
var deferredObject = $q.defer();
$http.post(
'/Account/Login', {
Email: emailAdress,
Password: password,
RememberMe: rememberMe
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true })
} else {
deferredObject.resolve({ success: false })
}
}).
error(function () {
deferredObject.resolve({ success: false })
});
return deferredObject.promise;
}
}]);
AAngScript.factory('RegistrationFactory', ['$http', '$q', function ($http, $q) {
return function (emailAdress, password, confirmPassword) {
var deferredObject = $q.defer();
$http.post(
'/Account/Register', {
Email: emailAdress,
Password: password,
ConfirmPassword: confirmPassword
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true });
} else {
deferredObject.resolve({ success: false });
}
}).
error(function () {
deferredObject.resolve({ success: false });
});
return deferredObject.promise;
}
}]);
AAngScript.config('appConfig', ['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
$routeProvider.
when('/register', {
templateUrl: 'Account/Register',
controller: 'RegisterController'
})
.when('/login', {
templateUrl: '/Account/Login.html',
controller: 'LoginController'
});
$httpProvider.interceptors.push('AuthHttpResponseInterceptor');
}]);
}());
总是出错[$injector:modulerr]
,真不知道该怎么办。
"Request" for Angular 模块位于部分视图 _Layout
中,代码为:
<html ng-app="AAngScript">
<head></head>
<body>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/angular-route.min.js"></script>
<script src="~/Scripts/AAngScript.js"></script>
....
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
谢谢你的帮助!
在你的最后一行 IIFE 没有正确关闭。你有:
}());
应该是:
})();
如:
(function ( /* IIFE enclosed code within this function*/) {
...
...
})(/* IIFE invoke*/);
此外,您正在将根本不应该注入的服务注入到您的配置中。您可以在配置函数中使用服务,而无需像在控制器和服务中那样显式注入它们,只需将它们作为函数参数。
为了促进更好的代码和实践,这是您使用接近 John Papa 的编码标准的代码 Angular Style Guide:
(function () {
// module definition
var AAngScript = angular.module('AAngScript', ['ngRoute']);
// Add controller to angular app
angular.module('AAngScript').controller('LoginController', LoginController);
// Inject dependencies to controller
LoginController.$inject = ['$routeParams', '$location', 'LoginFactory'];
// Define login controller
function LoginController($routeParams, $location, LoginFactory){
// vm as in view-model
var vm = this;
vm.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
vm.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}
// Add controller to angular app
angular.module('AAngScript').controller('RegisterController', RegisterController);
// Inject dependencies to controller
RegisterController.$inject = ['$location', 'RegistrationFactory'];
// Define login controller
$routeParams, $location, LoginFactory) {
$scope.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
$scope.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}]);
function RegisterController ($location, RegistrationFactory) {
// vm as in view-model
var vm = this;
vm.registerForm = {
emailAdress: '',
password: '',
confirmPassword: '',
registrationFailure: false
};
vm.register = function () {
var result = RegistrationFactory($scope.registerForm.emailAdress, $scope.registerForm.password, $scope.registerForm.confirmPassword);
result.then(function (result) {
if (result.success) {
$location.path('/routeOne');
} else {
$scope.registerForm.registrationFailure = true;
}
});
}
}
// Add factory to angular app
angular.module('AAngScript').factory('AuthHttpResponseInterceptor', AuthHttpResponseInterceptor);
// Inject dependencies to factory
AuthHttpResponseInterceptor.$inject = ['$q', '$location'];
// Define AuthHttpResponseInterceptor factory
function AuthHttpResponseInterceptor($q, $location) {
var factory = this;
factory.response = function (response) {
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
}
factory.responseError = function (rejection) {
if (rejection.status === 401) {
console.log("Response Error 401", rejection);
$location.path('/login').search('retutnUrl', $location.path());
}
return $q.reject(rejection);
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add factory to angular app
angular.module('AAngScript').factory('LoginFactory', LoginFactory);
// Inject dependencies to factory
LoginFactory.$inject = ['$http', '$q'];
// Define LoginFactory
function LoginFactory($http, $q) {
var factory = this;
factory.login = function(emailAdress, password, rememberMe){
var deferredObject = $q.defer();
$http.post(
'/Account/Login', {
Email: emailAdress,
Password: password,
RememberMe: rememberMe
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true })
} else {
deferredObject.resolve({ success: false })
}
}).
error(function () {
deferredObject.resolve({ success: false })
});
return deferredObject.promise;
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add factory to angular app
angular.module('AAngScript').factory('RegistrationFactory', RegistrationFactory);
// Inject dependencies to factory
RegistrationFactory.$inject = ['$http', '$q'];
// Define RegistrationFactory
function RegistrationFactory($http, $q) {
var factory = this;
factory.register = function(emailAdress, password, rememberMe){
var deferredObject = $q.defer();
$http.post(
'/Account/Register', {
Email: emailAdress,
Password: password,
ConfirmPassword: confirmPassword
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true });
} else {
deferredObject.resolve({ success: false });
}
}).
error(function () {
deferredObject.resolve({ success: false });
});
return deferredObject.promise;
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add configuration to angular app
angular.module('AAngScript').config(Routing);
// define configuration
function Routing($routeProvider, $httpProvider){
$routeProvider.
when('/register', {
templateUrl: 'Account/Register',
controller: 'RegisterController'
})
.when('/login', {
templateUrl: '/Account/Login.html',
controller: 'LoginController'
});
$httpProvider.interceptors.push('AuthHttpResponseInterceptor');
}
})();
很抱歉代码太长,但我的 AngularJS 模块有问题,该模块是根据 these lessons 创建的。我希望我的 angular 模块与 ASP.NET MVC 一起工作,但我什至不能 运行 它。
(function () {
var AAngScript = angular.module('AAngScript', ['ngRoute']);
AAngScript.controller('LoginController', ['$scope', '$routeParams', '$location', 'LoginFactory', function ($scope,
$routeParams, $location, LoginFactory) {
$scope.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
$scope.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}]);
AAngScript.controller('RegisterController', ['$scope', '$location', 'RegistrationFactory', function ($scope, $location, RegistrationFactory) {
$scope.registerForm = {
emailAdress: '',
password: '',
confirmPassword: '',
registrationFailure: false
};
$scope.register = function () {
var result = RegistrationFactory($scope.registerForm.emailAdress, $scope.registerForm.password, $scope.registerForm.confirmPassword);
result.then(function (result) {
if (result.success) {
$location.path('/routeOne');
} else {
$scope.registerForm.registrationFailure = true;
}
});
}
}]);
AAngScript.factory('AuthHttpResponseInterceptor', ['$q', '$location', function ($q, $location) {
return {
response: function (response) {
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
},
responseError: function (rejection) {
if (rejection.status === 401) {
console.log("Response Error 401", rejection);
$location.path('/login').search('retutnUrl', $location.path());
}
return $q.reject(rejection);
}
}
}]);
AAngScript.factory('LoginFactory', ['$http', '$q', function ($http, $q) {
return function (emailAdress, password, rememberMe) {
var deferredObject = $q.defer();
$http.post(
'/Account/Login', {
Email: emailAdress,
Password: password,
RememberMe: rememberMe
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true })
} else {
deferredObject.resolve({ success: false })
}
}).
error(function () {
deferredObject.resolve({ success: false })
});
return deferredObject.promise;
}
}]);
AAngScript.factory('RegistrationFactory', ['$http', '$q', function ($http, $q) {
return function (emailAdress, password, confirmPassword) {
var deferredObject = $q.defer();
$http.post(
'/Account/Register', {
Email: emailAdress,
Password: password,
ConfirmPassword: confirmPassword
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true });
} else {
deferredObject.resolve({ success: false });
}
}).
error(function () {
deferredObject.resolve({ success: false });
});
return deferredObject.promise;
}
}]);
AAngScript.config('appConfig', ['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
$routeProvider.
when('/register', {
templateUrl: 'Account/Register',
controller: 'RegisterController'
})
.when('/login', {
templateUrl: '/Account/Login.html',
controller: 'LoginController'
});
$httpProvider.interceptors.push('AuthHttpResponseInterceptor');
}]);
}());
总是出错[$injector:modulerr]
,真不知道该怎么办。
"Request" for Angular 模块位于部分视图 _Layout
中,代码为:
<html ng-app="AAngScript">
<head></head>
<body>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/angular-route.min.js"></script>
<script src="~/Scripts/AAngScript.js"></script>
....
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
谢谢你的帮助!
在你的最后一行 IIFE 没有正确关闭。你有:
}());
应该是:
})();
如:
(function ( /* IIFE enclosed code within this function*/) {
...
...
})(/* IIFE invoke*/);
此外,您正在将根本不应该注入的服务注入到您的配置中。您可以在配置函数中使用服务,而无需像在控制器和服务中那样显式注入它们,只需将它们作为函数参数。
为了促进更好的代码和实践,这是您使用接近 John Papa 的编码标准的代码 Angular Style Guide:
(function () {
// module definition
var AAngScript = angular.module('AAngScript', ['ngRoute']);
// Add controller to angular app
angular.module('AAngScript').controller('LoginController', LoginController);
// Inject dependencies to controller
LoginController.$inject = ['$routeParams', '$location', 'LoginFactory'];
// Define login controller
function LoginController($routeParams, $location, LoginFactory){
// vm as in view-model
var vm = this;
vm.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
vm.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}
// Add controller to angular app
angular.module('AAngScript').controller('RegisterController', RegisterController);
// Inject dependencies to controller
RegisterController.$inject = ['$location', 'RegistrationFactory'];
// Define login controller
$routeParams, $location, LoginFactory) {
$scope.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
$scope.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}]);
function RegisterController ($location, RegistrationFactory) {
// vm as in view-model
var vm = this;
vm.registerForm = {
emailAdress: '',
password: '',
confirmPassword: '',
registrationFailure: false
};
vm.register = function () {
var result = RegistrationFactory($scope.registerForm.emailAdress, $scope.registerForm.password, $scope.registerForm.confirmPassword);
result.then(function (result) {
if (result.success) {
$location.path('/routeOne');
} else {
$scope.registerForm.registrationFailure = true;
}
});
}
}
// Add factory to angular app
angular.module('AAngScript').factory('AuthHttpResponseInterceptor', AuthHttpResponseInterceptor);
// Inject dependencies to factory
AuthHttpResponseInterceptor.$inject = ['$q', '$location'];
// Define AuthHttpResponseInterceptor factory
function AuthHttpResponseInterceptor($q, $location) {
var factory = this;
factory.response = function (response) {
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
}
factory.responseError = function (rejection) {
if (rejection.status === 401) {
console.log("Response Error 401", rejection);
$location.path('/login').search('retutnUrl', $location.path());
}
return $q.reject(rejection);
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add factory to angular app
angular.module('AAngScript').factory('LoginFactory', LoginFactory);
// Inject dependencies to factory
LoginFactory.$inject = ['$http', '$q'];
// Define LoginFactory
function LoginFactory($http, $q) {
var factory = this;
factory.login = function(emailAdress, password, rememberMe){
var deferredObject = $q.defer();
$http.post(
'/Account/Login', {
Email: emailAdress,
Password: password,
RememberMe: rememberMe
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true })
} else {
deferredObject.resolve({ success: false })
}
}).
error(function () {
deferredObject.resolve({ success: false })
});
return deferredObject.promise;
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add factory to angular app
angular.module('AAngScript').factory('RegistrationFactory', RegistrationFactory);
// Inject dependencies to factory
RegistrationFactory.$inject = ['$http', '$q'];
// Define RegistrationFactory
function RegistrationFactory($http, $q) {
var factory = this;
factory.register = function(emailAdress, password, rememberMe){
var deferredObject = $q.defer();
$http.post(
'/Account/Register', {
Email: emailAdress,
Password: password,
ConfirmPassword: confirmPassword
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true });
} else {
deferredObject.resolve({ success: false });
}
}).
error(function () {
deferredObject.resolve({ success: false });
});
return deferredObject.promise;
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add configuration to angular app
angular.module('AAngScript').config(Routing);
// define configuration
function Routing($routeProvider, $httpProvider){
$routeProvider.
when('/register', {
templateUrl: 'Account/Register',
controller: 'RegisterController'
})
.when('/login', {
templateUrl: '/Account/Login.html',
controller: 'LoginController'
});
$httpProvider.interceptors.push('AuthHttpResponseInterceptor');
}
})();