Javascript 参数';myAppController';不是函数,未定义
我想将我的服务注入到我的控制器中,只需查看我的控制台日志(在Auth服务中),但我得到了以下错误:参数“myAppController”不是一个函数,未定义。我做错了什么 在main.js文件中,我有:Javascript 参数';myAppController';不是函数,未定义,javascript,angularjs,dependency-injection,Javascript,Angularjs,Dependency Injection,我想将我的服务注入到我的控制器中,只需查看我的控制台日志(在Auth服务中),但我得到了以下错误:参数“myAppController”不是一个函数,未定义。我做错了什么 在main.js文件中,我有: var myApp = angular.module('myApp',['ngRoute', 'ui.router']); myApp.controller('myAppController', ['$scope', function($scope, AuthService) { co
var myApp = angular.module('myApp',['ngRoute', 'ui.router']);
myApp.controller('myAppController', ['$scope', function($scope, AuthService) {
console.log('controller');
AuthService.console();
...
var myApp = angular.module('myApp',[]);
myApp.service('AuthService', function(){
this.console = function(){
console.log('in the AuthService');
}
});
在我的services.js文件中,我有:
var myApp = angular.module('myApp',['ngRoute', 'ui.router']);
myApp.controller('myAppController', ['$scope', function($scope, AuthService) {
console.log('controller');
AuthService.console();
...
var myApp = angular.module('myApp',[]);
myApp.service('AuthService', function(){
this.console = function(){
console.log('in the AuthService');
}
});
将文件加载到my index.html文件中,如下所示:
<script src="js/main.js"></script>
<script src="js/services.js"></script>
对于注入,也必须将其声明为字符串:
myApp.controller('myAppController', ['$scope', 'AuthService', function($scope, AuthService) {
也必须将其声明为用于注入的字符串:
myApp.controller('myAppController', ['$scope', 'AuthService', function($scope, AuthService) {
这样定义MyApp
var myApp = angular.module("myApp", ['ngRoute', 'ui.router']);
myApp.controller('myAppController', function ($scope, ajaxService) {
ajaxService.ajaxGetWithData(url).then(function (data) {
$scope.Data= data;
});
});
app.service('ajaxService', function ($http, $q) {
this.ajaxGetWithData = function (url) {
var deferred = $q.defer();
$http({
method: 'POST', url: url, data: $.param(User),
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
})
.success(function (data) {
if (data != null) {
deferred.resolve(data);
}
})
.error(function (err) {
deferred.reject(err);
});
return deferred.promise;
}
});
这样定义MyApp
var myApp = angular.module("myApp", ['ngRoute', 'ui.router']);
myApp.controller('myAppController', function ($scope, ajaxService) {
ajaxService.ajaxGetWithData(url).then(function (data) {
$scope.Data= data;
});
});
app.service('ajaxService', function ($http, $q) {
this.ajaxGetWithData = function (url) {
var deferred = $q.defer();
$http({
method: 'POST', url: url, data: $.param(User),
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
})
.success(function (data) {
if (data != null) {
deferred.resolve(data);
}
})
.error(function (err) {
deferred.reject(err);
});
return deferred.promise;
}
});
知道了!看起来我不需要在服务中重新声明myApp。所以我不需要这行代码:var myApp=angular.module('myApp',[]);在服务文件中使用
var myApp=angular.module('myApp')代码>。没有空的参数列表来告诉Angular使用现有的myApp而不是创建新的myApp。明白了!看起来我不需要在服务中重新声明myApp。所以我不需要这行代码:var myApp=angular.module('myApp',[]);在服务文件中使用var myApp=angular.module('myApp')代码>。没有空的参数列表来告诉Angular使用现有的myApp而不是创建新的myApp。