Javascript 在AngularJS中每次控制器调用之前清除一个变量?

Javascript 在AngularJS中每次控制器调用之前清除一个变量?,javascript,angularjs,livescript,Javascript,Angularjs,Livescript,如何在每次控制器调用之前设置一些模型/变量 目前,我有以下服务可帮助我在页面上设置错误消息(代码输入): 然后在我的index.html模板中: <div ng-show="error" class="alert alert-error">{{error}}</div> <div ng-show="success" class="alert alert-success">{{success}}</div> <div ng-view>

如何在每次控制器调用之前设置一些模型/变量

目前,我有以下服务可帮助我在页面上设置错误消息(代码输入):

然后在我的
index.html
模板中:

<div ng-show="error" class="alert alert-error">{{error}}</div>
<div ng-show="success" class="alert alert-success">{{success}}</div>
<div ng-view>        

有没有办法自动执行此清除步骤?

如果您想在每次路由更改后清除消息(这似乎符合您的使用情况),您可以将服务更改为以下内容:

angular.module "project.services" .factory "Message", ($rootScope) ->
  # The Message factory API.
  MessageApi = {
   error :   !(msg) -> $rootScope.error = msg
   success : !(msg) -> $rootScope.success = msg
   clear :   !(msg) -> 
               $rootScope.error = ''
               $rootScope.success = ''
  }

  # Call Message.clear whenever the route changes.
  $rootScope.$on '$routeChangeSuccess', -> MessageApi.clear!

  # Return the API.
  MessageApi

如果您想在每次路由更改后清除消息(这似乎适合您的用例),您可以将服务更改为以下内容:

angular.module "project.services" .factory "Message", ($rootScope) ->
  # The Message factory API.
  MessageApi = {
   error :   !(msg) -> $rootScope.error = msg
   success : !(msg) -> $rootScope.success = msg
   clear :   !(msg) -> 
               $rootScope.error = ''
               $rootScope.success = ''
  }

  # Call Message.clear whenever the route changes.
  $rootScope.$on '$routeChangeSuccess', -> MessageApi.clear!

  # Return the API.
  MessageApi
angular.module "project.services" .factory "Message", ($rootScope) ->
  # The Message factory API.
  MessageApi = {
   error :   !(msg) -> $rootScope.error = msg
   success : !(msg) -> $rootScope.success = msg
   clear :   !(msg) -> 
               $rootScope.error = ''
               $rootScope.success = ''
  }

  # Call Message.clear whenever the route changes.
  $rootScope.$on '$routeChangeSuccess', -> MessageApi.clear!

  # Return the API.
  MessageApi