angularjs应用程序服务(…)原因“;未捕获类型错误:未定义不是函数;
在以前的项目中运行良好的内容会导致当前项目中出现错误。 我为以下各种任务提供了几项服务:angularjs应用程序服务(…)原因“;未捕获类型错误:未定义不是函数;,angularjs,Angularjs,在以前的项目中运行良好的内容会导致当前项目中出现错误。 我为以下各种任务提供了几项服务: angular.module('myApp') .service('test1', function() { return { sayHello : function() { return ("Hello!"); } }; }) .service('test2', f
angular.module('myApp')
.service('test1', function() {
return {
sayHello : function() {
return ("Hello!");
}
};
})
.service('test2', function() { //this line causes the error
return {
sayHi : function() {
return ("Hi!");
}
};
})
.service('test3', function() {
...
})
app.service = $provide.service;
var app = angular.module('myApp'); // this is already done in the main js.
// define all services separately
app.service('test1', function() {...});
app.service('test2', function() {...});
app.service('test3', function() {...});
当我在项目中包含此文件时,我在“.service('test2',function()…”行中收到以下错误消息:
未捕获类型错误:未定义不是函数
无论我在做什么-第二个方法总是导致此错误。看起来第一个服务方法没有返回对象?谢谢!看起来您缺少一些右括号,因此第二个服务中似乎发生的错误实际上是由第一个服务中的语法错误引起的 试试这个:
angular.module('myApp')
.service('test1', function() {
return {
sayHello : function() {
return ("Hello!");
}
};
})
.service('test2', function() {
return {
sayHi : function() {
return ("Hi!");
}
};
});
我现在发现了问题所在。我正在开发一个由其他人(CLIP-TWO主题)启动的应用程序,他在路由器配置文件中将所有服务定义为提供者。如下所示:
angular.module('myApp')
.service('test1', function() {
return {
sayHello : function() {
return ("Hello!");
}
};
})
.service('test2', function() { //this line causes the error
return {
sayHi : function() {
return ("Hi!");
}
};
})
.service('test3', function() {
...
})
app.service = $provide.service;
var app = angular.module('myApp'); // this is already done in the main js.
// define all services separately
app.service('test1', function() {...});
app.service('test2', function() {...});
app.service('test3', function() {...});
当我现在定义我的服务时,第一个可以工作,但第二个不能,因为第二个不在应用程序范围内,而是在提供者范围内。
因此,我不得不这样更改我的服务定义:
angular.module('myApp')
.service('test1', function() {
return {
sayHello : function() {
return ("Hello!");
}
};
})
.service('test2', function() { //this line causes the error
return {
sayHi : function() {
return ("Hi!");
}
};
})
.service('test3', function() {
...
})
app.service = $provide.service;
var app = angular.module('myApp'); // this is already done in the main js.
// define all services separately
app.service('test1', function() {...});
app.service('test2', function() {...});
app.service('test3', function() {...});
尝试使用此声明服务方法
angular.module('myApp')
.service('test1', function() {
this.sayHello = function() {
return 'Hello';
};
});
谢谢-我更改了我的示例。很抱歉,真正的代码没有缺少括号。它仍然不起作用。