angularjs app.service(...)导致“未捕获的TypeError:undefined不是函数”

时间:2015-07-05 00:18:03

标签: angularjs

在以前的项目中运作良好会导致当前项目出错。 我为这样的各种任务提供了几项服务:

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() {
        ...
    })

当我在项目中包含此文件时,我在“.service('test2',function()...”行中收到以下错误消息:

未捕获的TypeError:undefined不是函数

无论我在做什么 - 第二种方法总是会导致此错误。看起来第一个服务方法不返回对象?谢谢!

3 个答案:

答案 0 :(得分:2)

看起来你错过了一些右括号,所以第二个服务中似乎发生的错误实际上是由第一个服务中的语法错误引起的。

试试这个:

angular.module('myApp')
.service('test1', function() {
    return {
        sayHello : function() {
             return ("Hello!");
        }
    };      
})
.service('test2', function() {
    return {
        sayHi : function() {
             return ("Hi!");
        }
    };      
});

答案 1 :(得分:0)

我现在发现了什么问题。我正在开发一个由其他人(CLIP-TWO Theme)启动的应用程序,该应用程序将所有服务定义为其路由器配置文件中的提供程序。就是这样:

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() {...});

当我现在定义我的服务时,第一个服务将起作用,但第二个服务不起作用,因为第二个不在应用程序范围内但在提供者范围内。 所以我不得不改变我的服务定义:

{{1}}

答案 2 :(得分:0)

尝试用此声明服务方法。

/tmp/cca1J32y.o: In function `downloadFile(char const*, char const*)':
test.cpp:(.text+0x35): undefined reference to `curl_easy_init'
test.cpp:(.text+0x72): undefined reference to `curl_easy_setopt'
test.cpp:(.text+0x8d): undefined reference to `curl_easy_setopt'
test.cpp:(.text+0xa7): undefined reference to `curl_easy_setopt'
test.cpp:(.text+0xb2): undefined reference to `curl_easy_perform'
test.cpp:(.text+0xc0): undefined reference to `curl_easy_cleanup'
collect2: error: ld returned 1 exit status