2017-03-22 10 views
0

POST、PUT、DELETEの呼び出しが完了したときにメッセージをブロードキャストするシンプルなファクトリがあります。その後、コントローラーの中で、3つのすべての呼び出しで同じ動作を行い、実行します。 1つのステップで3つのイベントをすべて聞くことはできますか?スコープ内で複数のイベントを聴く

工場で

、コントローラ(私が持っているもの)コントローラで

$scope.$on('putMsg', (e, putResp) => ....long function chain..) 
$scope.$on('postMsg', (e, postResp) => ....long function chain..) 
$scope.$on('delMsg', (e, delResp) => ....long function chain..) 

(私が欲しいもの)
$scope.$on('{putMsg,postMsg,delMsg}', (e, {putRes,postResp,delResp}) => ....long function chain..)

される関数チェーンで

$http.method(something) 
.then(response => $rootScope.$broadcast('putpostdelMSG', response)) * 3 lines 

doneは、3つの方法のいずれかからブロードキャストを聴いても同じです。短縮する方法とコードを繰り返さない方法はありますか?ありがとう

答えて

1

セントラルイベントハンドラが必要な場合はこれですが、私は懸念を分離すると思いますが、別のイベントハンドラを呼び出すことはOKです。同じ基本機能を呼び出すだけです。

var app = angular.module("app", []); 
 
app.controller("exampleController", function($rootScope, $scope) { 
 
    $scope.Get = function() { 
 
    $rootScope.$broadcast("receivedMsg", "get data", "GET"); 
 
    } 
 
    $scope.Post = function() { 
 
    $rootScope.$broadcast("receivedMsg", "update data", "POST"); 
 
    } 
 

 
    $scope.$on("receivedMsg", function(e, response, requestMethod) { 
 
    if (requestMethod === "GET") 
 
     $scope.show = response 
 
    else 
 
     $scope.show = response 
 

 
    }) 
 

 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="exampleController"> 
 
    <button type='button' ng-click="Get()">GET</button> 
 
    <button type='button' ng-click="Post()">POST</button> {{show}} 
 
</div>

+0

すごいことには、複数のparamsを渡すのclean.never考えです。どうもありがとう – Jamie

関連する問題