$ EMITまたはサービスからの$ブロードキャストイベントとそれらをコントローラ(またはいくつか)でそれらを聴く

StackOverflow https://stackoverflow.com//questions/23013883

質問

私はサービスからイベントを発信/ブロードキャストしたい簡単な例を符号化しており、そのイベントをコントローラで聞いてUIを変更することをお勧めしますが、それを作業したり、コードをデバッグすることはできません。リスナーに停止しているようですが、機能を実行しません。

http://plnkr.co/edit/eglcq7zellfkp86dyzoe?p=preview

サービス:

angular.module('ServiceModule', []).
service('servicetest', ['$rootScope', function($rootScope){
    this.test = function(){
      $rootScope.$emit('testevent');
    };
}]);
.

コントローラ

angular.module('ControllerModule', ['ServiceModule']).
    controller('ControllerTest', ['$scope','$rootScope','servicetest', function($scope, $rootScope, servicetest){
      $scope.name = 'World';
      servicetest.test();
      $rootScope.$on('testevent', function(){
        $scope.name = 'Joe';
      });
    }]);
.

index

<!DOCTYPE html>
<html ng-app="ControllerModule">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js" data-semver="1.2.16"></script>
    <script src="controller.js"></script>
    <script src="service.js"></script>
  </head>

  <body ng-controller="ControllerTest">
    <p>Hello {{name}}!</p>
  </body>

</html>
.

溶液:

IvarniまたはWalterブランドが報告された場合、リスナーの後にイベントをトリガーするサービス機能の呼び出しは、リスナーがまだ聴くことができるイベントを起動していない場合は

次のようにコントローラを変更する必要があります。

サービス

angular.module('ControllerModule', ['ServiceModule']).
        controller('ControllerTest', ['$scope','$rootScope','servicetest', function($scope, $rootScope, servicetest){
          $scope.name = 'World';
          $rootScope.$on('testevent', function(){
            $scope.name = 'Joe';
          });
          servicetest.test();
        }]);
.

役に立ちましたか?

解決

リスナーを接続する前にイベントを起動しています。

これを試してみてください:

  $rootScope.$on('testevent', function(){
    $scope.name = 'Joe';
  });
  servicetest.test();
.

他のヒント

このように、コントローラのリスナーの下にServiceTest.test()を配置するだけです。

  $rootScope.$on('testevent', function(){
    $scope.name = 'Joe';
  });
  servicetest.test();
.

Test()を呼び出した直後にリスナーを添付し、イベントを見逃しているだけです。

中規模のアプリで最もよく機能することが判明した解決策は、$ EMIT関数の周りにラッパーを作成し、すべてのイベントを登録するのに十分な数ミリ秒で$ EMITを遅らせることです。

現在の$範囲を渡すように、ラッパーにもっと多くのグッズを紹介することも、$ ROOTSCOPEの新しい子スコープを作成してください(これは、その下に1つのレベルになるので、伝播するときそれは依然として速くなるでしょう。$ ROOTSCOPEの最大$ ROOTSCOPE)、およびデータチャネルとしてそれを使用して、スコープダイの後のイベント破壊など...

これはソースコード全体です。> http://jsfiddle.net/gabrielcatalin/2ur7

そしてこれが抜粋です:

/**
 * Waits a given amount of time before calling emitting
 * This works well when the event registration happens before emitting.
 *
 * @param name
 * @param fn
 * @param wait
 */
this.delayEmit = function (name, fn, wait) {
    // Because of the $scope's lifecycle the $emit(publish) happens after the $on(subscription)
    //  therefore that needs to be taken care of with a delay
    $timeout(function () {
        self.emit(name, fn);
    }, wait || 100);
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top