I'm currently attempting to retrieve a row count value from a web API using a service from one controller, and then update a different controller's variable after the value has been retrieved from the DB - all while avoiding using $scope or $rootScope.
Below is Controller1 - this controller needs to update its variable when the value in the service changes. Currently it operates correctly, but I would like to avoid using $scope or $rootScope:
(function() {
'use strict';
angular
.module('app.event')
.controller('Controller1', Controller1);
Controller1.$inject = ['service1', '$stateParams', '$rootScope'];
/**
* Controller 1
* @constructor
*/
function Controller1(service1, $stateParams, $rootScope) {
// Declare self and variables
var vm = this;
vm.number = 0;
init();
$rootScope.$on('countChanged', refresh);
/**
* Initializes the controller
*/
function init() {
service1.refreshCount($stateParams.id);
}
/**
* Refreshes the count
* @param {object} event - The event returned from the broadcast
* @param {int} count - The new count to update to
*/
function refresh(event, count) {
vm.number = count;
}
}
})();
The service - I'd like to avoid using $rootScope.$broadcast here:
(function() {
'use strict';
angular
.module('app.event')
.factory('service1', service1);
service1.$inject = ['APP_URLS', '$http', '$rootScope'];
/**
* The service
* @constructor
*/
function service1(APP_URLS, $http, $rootScope) {
// Declare
var count = 0;
// Create the service object with functions in it
var service = {
getCount: getCount,
setCount: setCount,
refreshCount: refreshCount
};
return service;
///////////////
// Functions //
///////////////
/**
* Re-calls the web API and updates the count
* @param {Guid} id - The ID needed for the API call parameter
*/
function refreshCount(id) {
$http({ url: APP_URLS.api + '<TheAPINameHere>/' + id, method: 'GET' }).then(function (response) {
setCount(response.data.Count);
changed();
});
}
/**
* Returns the count value
*/
function getCount() {
return count;
}
/**
* Re-calls the web API and updates the count
* @param {int} newCount - The new count value
*/
function setCount(newCount) {
count = newCount;
changed();
}
/**
* Broadcasts a change event to be picked up on in Controller1
*/
function changed() {
$rootScope.$broadcast('countChanged', count);
}
}
})();
Below is a function in Controller2 that updates the value in the service - I'd like to be able to get the value in Controller1 to update as soon as I do this:
/**
* Removes a row from the database
* @param {object} field - The data row object that we're deleting from the table
*/
function remove(field) {
// Delete the row from the database
service2.delete(field.Id);
// Remove the row from the local data array and refresh the grid
vm.data.splice(vm.data.indexOf(field), 1);
// Set the count in the service to update elsewhere
service1.setCount(vm.data.length);
vm.indices.reload();
}
I was avoiding using $rootScope for practice purposes, however it seems to be the best way to go. The public property wasn't working for me when I attempted to use it per some of the other suggestions, so I went with using $rootScope.$emit like CainBot suggested.