Reputation: 3921
If the user pushes the button for saving, the following logic occurs:
Popup.notifyPopup($scope, "Saved...")
$location.path('/changeView');
Popup.notifyfyPopup function is the function for setting the message in the second parameter into the scope and notifying the message in the notify popup.
function notifyTopNotification(scope, msg) {
sharedProperties.setNotifyMsg(msg);
ngDialog.open({
template: 'notifyTopNotification',
controller: function() {4
$scope.notifyMsg = sharedProperties.getNotifyMsg();
},
className: 'ngdialog-close',
scope: $scope
});
setTimeout(1000);
};
notifyPopup : function(scope, msg) {
$scope = scope;
notifyTopNotification($scope, msg);
}
Mostly, It works. But the function followed by the location.path() doesn't work. Because the location.path() function changed the scope. How can I do this?
Upvotes: 0
Views: 77
Reputation: 6221
If you are trying to pass messages between scopes, you may want to use $broadcast to send and $on to listen for the broadcast. But be aware that you must have the receiving controller instantiated already, or it won't work. In this case, you would initiate the state change first (which would instantiate the receiving controller) and then $broadcast the message.
The following works if there is a parent/child relationship between scopes:
function firstCtrl($scope){
$scope.$broadcast('someEvent', [1,2,3]);
}
function secondCtrl($scope){
$scope.$on('someEvent', function(event, mass) {console.log(mass)});
}
But if there is none, you need to use $rootScope:
function firstCtrl($rootScope){
$rootScope.$broadcast('someEvent', [1,2,3]);
}
Broadcast fires messages from parent scopes down to child scopes. If you need to send messages upstream, you do the same thing but you need to use $emit instead of $broadcast.
More information is available in the docs as well: https://docs.angularjs.org/api/ng/type/$rootScope.Scope
Upvotes: 1