Friday, September 29, 2017

Using $rootScope and $broadcast to communicate between functions

In my app I have one page that shows the details of a particular users document in which a document can be deleted and another that shows the users recently viewed documents. I’m trying to make it so when a user deletes a document in the view page it also deletes the listing in the users recently viewed documents history.

I’m attempting to use $broadcast and $rootScope to communicate between the two modules but I suspect I’m doing something wrong with the syntax or there’s an issue with scope or project structure.

Here’s the two separate functions that delete the different entries

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

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/09/29/using-rootscope-and-broadcast-to-communicate-between-functions/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment