在 angularjs 中的指令中使用服务函数
Use service function in directive in angularjs
我想从指令的控制器调用服务函数。服务名称是 Auth,函数名称是 Auth.logout。我已经在指令的控制器中注入了服务但没有工作。请让我知道我该怎么办?该脚本是用 coffeescript 编写的。
指令代码:-
class GlobalNav extends BaseDirective
@register 'globalNav', ->
restrict: "E"
templateUrl: 'templates/Directives/global-nav.jade'
controller: ($scope, $ionicSideMenuDelegate, $ionicPopup,Auth) ->
$scope.closeSideMenu = ->
$ionicSideMenuDelegate.toggleLeft(false)
$scope.showConfirm = ->
confirmPopup = $ionicPopup.confirm(
title: 'Message'
template: 'Are you sure you want to logout?')
confirmPopup.then (res) ->
if res
Auth.logout <<<<<<<<<<<<<<<<<<<<< This is not called
return
return
授权服务代码:-
class Auth extends BaseService
@register 'Auth'
AUTH_KEY: 'userAuth'
AUTH_REJECTION: 'authentication'
constructor: (@$q, @$http, @$rootScope, @$cordovaFacebook, @storage, @Api, @$track, @FacebookAuth) ->
authenticated: -> !_.isEmpty(@_getAuth())
logout: ->
q = @$q.defer()
@storage.clearAll()
@_auth = null
@Api.expireGlobalCache()
@$cordovaFacebook.logout().then(q.resolve)
q.promise
翻译成JavaScript后,原来你没有调用Auth.logout()
,而只计算Auth.logout
(没有应用()
运算符)。
我想从指令的控制器调用服务函数。服务名称是 Auth,函数名称是 Auth.logout。我已经在指令的控制器中注入了服务但没有工作。请让我知道我该怎么办?该脚本是用 coffeescript 编写的。
指令代码:-
class GlobalNav extends BaseDirective
@register 'globalNav', ->
restrict: "E"
templateUrl: 'templates/Directives/global-nav.jade'
controller: ($scope, $ionicSideMenuDelegate, $ionicPopup,Auth) ->
$scope.closeSideMenu = ->
$ionicSideMenuDelegate.toggleLeft(false)
$scope.showConfirm = ->
confirmPopup = $ionicPopup.confirm(
title: 'Message'
template: 'Are you sure you want to logout?')
confirmPopup.then (res) ->
if res
Auth.logout <<<<<<<<<<<<<<<<<<<<< This is not called
return
return
授权服务代码:-
class Auth extends BaseService
@register 'Auth'
AUTH_KEY: 'userAuth'
AUTH_REJECTION: 'authentication'
constructor: (@$q, @$http, @$rootScope, @$cordovaFacebook, @storage, @Api, @$track, @FacebookAuth) ->
authenticated: -> !_.isEmpty(@_getAuth())
logout: ->
q = @$q.defer()
@storage.clearAll()
@_auth = null
@Api.expireGlobalCache()
@$cordovaFacebook.logout().then(q.resolve)
q.promise
翻译成JavaScript后,原来你没有调用Auth.logout()
,而只计算Auth.logout
(没有应用()
运算符)。