【发布时间】:2016-01-12 15:49:22
【问题描述】:
我正在尝试使用以下示例代码为数据库访问定义一个工厂:
MyApp.service("DB_Services", [ "$http" , function($http) {
DB_Services.Get_Data_from_DB_Internal = function (p_Request) {
var http = new XMLHttpRequest();
var url = "http://localhost/servlet/Test_ui";
var params = "data=" + p_Request ;
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
if(http.readyState == 4 && http.status == 200) {
return http.responseText ;
}
}
http.send(params);
return DB_Services ;
}]);
我应该补充一点,这段代码部署在一个单独的文件中,该文件列在 index.html 中的 <script src=...> 条目中。
加载时(以及在使用它之前!),我收到以下错误(Chorme 的控制台):未捕获的 ReferenceError: MyApp 未定义。
当然,MyApp 与所有控制器中使用的名称完全相同,因此不会出现拼写错误。
此外,如果我将“MyApp”更改为其他内容,例如“TestName”,我会收到相同的错误,只是这次未定义的是 TestName。
我无法弄清楚语法错误是什么。
【问题讨论】:
-
如何通过 MyApp 引用您的模块名称?
MyApp = angular.module('myApp')?以及MyApp的范围是如何设置的? -
MyApp定义在哪里?
标签: javascript angularjs factory