2014-01-17 54 views
1

這是關於樣式的問題,或角方式AngularJS:傳遞消息到路由

當用戶註冊後,我向他們發送一封確認郵件重定向到'/'

此外,登錄時,如果用戶沒有確認他們的電子郵件,我給他們一個選項,以重新發送確認電子郵件。我重新發送確認郵件重定向到'/'

在這兩種情況下,我想在主頁的頂部顯示一條信息性消息

我已經找到一種方法來做到這一點。

我創造出有blank template和簡單controller它設置在service消息並重定向到'/'一些虛擬路線:

它只是感覺有點hackish的。必須指定一個非空的template尤其難看。

我敢肯定有必須有一個更好的方式來做到這一點?一些如何將$scope重定向到控制器,或者沿着這些線路。

這是我實現

虛擬路線:

app.config(function($routeProvider) { 
    $routeProvider 
     .when('/confirm', { 
      template: " ", 
      controller: function(Message, $location) { 
       Message.info = "We've sent you a confirmation email - please check your inbox"; 
       $location.path('/'); 
      } 
     }) 
     .when('/reconfirm', { 
      template: " ", 
      controller: function(Message, $location) { 
       Message.info = "We've resent your confirmation email - please check your inbox"; 
       $location.path('/'); 
      } 
     }) 

消息服務:

app.factory('Message', function() { 
    this.message = { 
     info: "", 
    }; 
    return this.message; 
}); 

爲了完整起見,這裏是消息局部控制器

部分(拉入index.htmlng-include):

<div class="container" ng-controller="MessageCtrl"> 
     <div ng-show="message.info" class="alert alert-info"> 
      {{ message.info }} 
      <button class="close pull-right" ng-click="message.info=''">&times;</button> 
     </div> 
    </div> 

控制器

app.controller('MessageCtrl', function($scope, Message) { 
    $scope.message = Message; 
}); 

回答

3

你可以使用$發射/ $上做到這一點。

可以使用MessageCtrl來顯示使用$事件觸發

$scope.$on('EventName', function(event, anyInput) { 
    // Do your magic 
} 

從其他控制器發送您做$放出消息的消息。如果你願意,你也可以使用$ rootScope來使它成爲全局的。

$scope.$emit('EventName', inputToThe$on); 

入住此的jsfiddle:http://jsfiddle.net/4wHgh/

這裏是$另一個很好的解釋放出和$播放:Working with $scope.$emit and $scope.$on

希望這是一個有用的選擇。

+0

這是一個很好的選擇! –