AngularJs - RXJS Observable 退订

AngularJs - RXJS Observable unsubscribe

我已经设置了一个 RXJS 可观察对象。我有两个组件订阅了服务工厂中的一个主题。我如何取消订阅主题的选定组件,以便按下按钮停止收听主题广播?

查看我的 jsfiddle Unsubscribe App

我的代码:

<div ng-app="myApp" ng-controller="mainCtrl">

  <script type="text/ng-template" id="/boxa">
  BoxA - Message Listener: </br>
  <strong>{{boxA.msg}}</strong></br>
  <md-button ng-click='boxA.unsubcribe()' class='md-warn'>Unsubscribe A</md-button>
  </script>
  <script type="text/ng-template" id="/boxb">
    BoxB - Message Listener: </br>
  <strong>{{boxB.msg}}</strong></br>
  <md-button ng-click='boxB.unsubcribe()' class='md-warn'>Unsubscribe B</md-button>
 </script>

  <md-content class='md-padding'>
    <h3>
      {{name}}
    </h3>
    <label>Enter Text To Broadcast</label>
    <input ng-model='msg'/></br>
    <md-button class='md-primary' ng-click='broadcastFn()'>Broadcast</md-button></br>
    <h4>
    Components
    </h4>
    <box-a></box-a></br>
    <box-b></box-b>
  </md-content>

</div><!--end app-->


var app = angular.module('myApp', ['ngMaterial']);
app.controller('mainCtrl', function($scope,msgService) {

   $scope.name = "Observer App Example";
   $scope.msg = 'Message';
   $scope.broadcastFn = function(){
        msgService.broadcast($scope.msg);
   }   
});

app.component("boxA",  {
      bindings: {},
      controller: function(msgService) {
        var boxA = this;
        boxA.msgService = msgService;            
        boxA.msg = '';
        boxA.msgService.subscribe(function(obj) { 
            console.log('Listerner A');
          boxA.msg = obj;
                });
        boxA.unsubscribe=function(){

        };

      },
      controllerAs: 'boxA',
      templateUrl: "/boxa"
})
app.component("boxB",  {
      bindings: {},
      controller: function(msgService) {
        var boxB = this;
        boxB.msgService = msgService;            
        boxB.msg = '';
        boxB.msgService.subscribe(function(obj) { 
            console.log('Listerner B');
          boxB.msg = obj;
                });

        boxB.unsubscribe=function(){

        };
      },
      controllerAs: 'boxB',
      templateUrl: "/boxb"
})

app.factory('msgService', ['$http', function($http){
    var msgSubject = new Rx.ReplaySubject();
    return{
        subscribe:function(subscription){
            msgSubject.subscribe(subscription);
        },
        broadcast:function(msg){
        console.log('success');
            msgSubject.onNext(msg);
        }
    }   
}])

请查看更新后的 fiddle:here

subscribe 函数 return 是一个 Disposable 要使用的,您必须首先 return 来自您的工厂的订阅(第 60 行):

subscribe: function(subscription){
    return msgSubject.subscribe(subscription);
}

这样您就可以将订阅存储在每个控制器中,以便将来使用。 (第 21 和 42 行)

var boxASubscription = boxA.msgService.subscribe(function(obj) {
    console.log('Listerner A');
    boxA.msg = obj;
});

您可以在想要取消订阅时调用订阅的 dispose 方法:

boxA.unsubscribe = function(){
    console.log('Unsubscribe A');
    boxASubscription.dispose();
};

n.b.

出于某种原因,我无法让您的演示与 <md-button> 一起使用,因此为了演示,我将其更改为 <button>

根据我上面的评论,新的 RxJs 5 Beta 现在从 subscription.dispose() 更改为 subscription.unsubscribe() 请参阅此处 https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#subscription-dispose-is-now-unsubscribe