提问者:小点点

AngularJs-RXJS可观察退订


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

查看我的jsfiddle退订应用程序

我的代码:

<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);
        }
    }   
}])

共2个答案

匿名用户

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

匿名用户

请看更新的小提琴:这里

订阅函数返回一个Disposable来使用,您必须首先从工厂返回订阅(第60行):

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

这将允许您将订阅存储在每个控制器中,以便将来使用。(第21行

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

然后,当您想取消订阅时,您可以调用订阅上的disple方法:

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

n. b.

由于某种原因,我无法让您的演示与一起使用

相关问题