为什么我的AngularJS服务不能公开它的功能?

时间:2014-05-08 21:25:19

标签: angularjs coffeescript angularjs-service

我的服务是:

myApp.service 'userService', [
  '$http'
  '$q'
  '$rootScope'
  '$location'
  ($http, $q, $rootScope, $location) ->
    deferred = $q.defer()

    @initialized = deferred.promise

    @user =
      access: false

    @isAuthenticated = ->
      @user =
        first_name: 'Shamoon'
        last_name: 'Siddiqui'
        email: 'ssiddiqui@liquidnet.com'
        access: 'institution'

      deferred.resolve()
]

我将其加载到.run中,如下所示:

myApp.run [
  '$rootScope'
  'userService'
  ($rootScope, userService) ->

    userService.isAuthenticated().then (response) ->
      if response.data.user
        $rootScope.$broadcast 'login', response.data
      else
        userService.logout()
]

但是萤火虫告诉我:

TypeError: userService.isAuthenticated is not a function
    return userService.isAuthenticated().then(function(response) {

不确定我做错了什么。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的脚本编译为:

return this.isAuthenticated = function() {
 ...

这意味着userService实际上是isAuthenticated函数。 只需在return函数的末尾添加userService语句。