2017-09-29 10 views
0

私のアプリでは、ある文書を削除できる特定のユーザ文書の詳細を表示するページと、最近閲覧したユーザを示す別のページがあります。私はそれを作るようにしようとしているので、ユーザがビューページ内のドキュメントを削除すると、ユーザが最近閲覧したドキュメント履歴のリストも削除されます。

2つのモジュール間の通信に$ broadcastと$ rootScopeを使用しようとしていますが、私は構文に何か間違っていると思われます。スコープやプロジェクト構造に問題があります。

ここにあなたのrootScope注入は、あなたの罰金であることを考えると

history.js

//gets the selected item the user clicks and deletes it and updates history 
$scope.removeFavorite = function(item) { 
    var items = $scope.recent[item.type]; 

    item = items.splice(items.indexOf(item), 1)[0]; 

    $rootScope.$on('deleteRecent', function(data) { 
    historyManager.remove(data); 
    }); 

    historyManager.remove(item).then(loadHistoryItems, loadHistoryItems); 
}; 

documentView.js

//confirmation that user wants selected document deleted 
function confirmDelete() { 
    var delObj = _.pick(sDocument, 'Doc_Type', 'Doc_Num'); 

    repos 
    .sDoc 
    .del(delObj); 

    var item = sDocument.Doc_Type + ';' + sDocument.Doc_Num; 

    $rootScope.$broadcast('deleteRecent', item); 

    tabBarViewModel.removeTabByState($state.get('sDocument'), delObj); 

    $scope.modalOptions.hide(); 
} 
+0

あなたはモジュールを言うとき、あなたはコントローラを意味するのですか?そして、そうであれば、なぜサービスがAnglesJSの意図する通信手段である場合に、サービスの代わりにイベントを使用していますか? –

+0

'$ broadcast'を使うのは悪い習慣です。可能であれば私はそれを避けるだろう。 – Jax

+0

私はコントローラを使用しています、私はお詫びします、私はまだAngularにはかなり新しいです。ですから、私が呼んでいる$ブロードキャスト機能の代わりに動作するサービスを作成する必要がありますか? – essenbsa

答えて

0

異なるエントリを削除し、2つの独立した機能です問題のある部分はこれです:

$scope.removeFavorite = function(item) { 
    var items = $scope.recent[item.type]; 

    item = items.splice(items.indexOf(item), 1)[0]; 

    $rootScope.$on('deleteRecent', function(data) { 
    historyManager.remove(data); 
    }); 

    historyManager.remove(item).then(loadHistoryItems, loadHistoryItems); 
}; 

最初に$ rootScopeをブロードキャストすると、$ scopeでメッセージを受け取ることができます。それ以外の場合は$ scopeの関数にラップしないで、最初のパラメータはeventです。

だから、正しいコードは次のようになります。

$scope.$on('deleteRecent', function(event, data) { 
    historyManager.remove(data); 
}); 

$scope.removeFavorite = function(item) { 
    var items = $scope.recent[item.type]; 

    item = items.splice(items.indexOf(item), 1)[0]; 



    historyManager.remove(item).then(loadHistoryItems, loadHistoryItems); 
}; 
関連する問題