I am new to angularjs and I am angularjs web application to gain experience and practice. The problem is that $scope.$emit does not seem to work, I'm looking for ways to connect functions between controllers, and so far I have found on the Internet that $scope.emit and $scope.on seem to be suitable for this type of task if there is another way, I would like to know one way or another the code is written as follows:
loginController.js
(function(angular) { var app = angular.module('Organizer'); // inject $scope to the controller in order to try $scope.$emit app.controller('login', function($http, $scope) { // i define the scope like this so i can access inside other functions var scope = this; scope.processing = false; scope.message = null; scope.submit = function() { scope.processing = true; // store data for post var post = { username: scope.username, password: scope.password }; // send post data $http.post('api/default/login', post). success(function(data, status) { // hide processing feedback and show the result scope.processing = false; scope.message = data.message; }). error(function(data, status) { scope.processing = false; }); }; // Function i use to emit this.closeDialog = function() { $scope.$emit('closeDialog'); }; }); })(angular);
siteController.js
(function(angular) { var app = angular.module('Organizer'); app.controller('site', function($mdDialog, $scope) { this.menu = ['test1', 'test2']; this.dialog = function() { $mdDialog.show({ templateUrl: 'site/login', }); };
Note: I am using angular material, and you can see that I am showing a dialog that is login, the login has its own controller (I wanted it to use the same site controller, but I do not know how to do this), and in this there is a button in the dialog that calls the closeDialog() function in loginControler and should close the dialog, but now, for testing reasons, I just register if it triggers an event
source share