Javascript 错误:Argumentcontroller不是函数,未定义
我使用Angular踢轮胎,在运行沙盒应用程序时遇到问题。我想使用服务实现一个简单的控制器。但是,我收到以下错误: 错误:参数“UserController”不是函数,未定义 这些文件在页面上按列出的顺序引用。 webConfig.JSJavascript 错误:Argumentcontroller不是函数,未定义,javascript,angularjs,Javascript,Angularjs,我使用Angular踢轮胎,在运行沙盒应用程序时遇到问题。我想使用服务实现一个简单的控制器。但是,我收到以下错误: 错误:参数“UserController”不是函数,未定义 这些文件在页面上按列出的顺序引用。 webConfig.JS var app = angular.module('app', []); UserController.JS app.controller('UserController', function ($scope, UserService) { // De
var app = angular.module('app', []);
UserController.JS
app.controller('UserController', function ($scope, UserService) {
// Define the model properties. The view will loop
// through the services array and genreate a li
// element for every one of its items.
$scope.services = [
{
name: 'Web Development',
price: 300,
active: true
}, {
name: 'Design',
price: 400,
active: false
}, {
name: 'Integration',
price: 250,
active: false
}, {
name: 'Training',
price: 220,
active: false
}
];
$scope.LogInfo = {
UserName: 'username',
Password: 'password'
};
$scope.ProcessLogin = function (Info) {
UserService.GetLoginStatus(Info);
};
$scope.toggleActive = function (s) {
s.active = !s.active;
};
// Helper method for calculating the total price
$scope.total = function () {
var total = 0;
// Use the angular forEach helper method to
// loop through the services array:
angular.forEach($scope.services, function (s) {
if (s.active) {
total += s.price;
}
});
return total;
};
});
UserService.JS
app.service('UserService', function () {
this.GetLoginStatus = function (Info) {
alert(JSON.stringify(Info))
};
});
由于您的应用程序跨越多个文件,因此我不会通过
app
变量将您的控制器添加到模块中,而是通过其名称查找模块
更改:
app.controller('UserController', function ($scope, UserService) {
&
app.service('UserService', function () {
致:
我想出来了。这个问题实际上源于我的HTML。 一旦我在引用控制器的地方添加了ng app=“app”,那么一切都正常了
<body ng-controller="UserController" ng-app="app">
@Bronco感谢您的建议,但我得到了相同的结果。您的文件是否正在缩小?否文件未缩小。
<body ng-controller="UserController" ng-app="app">