I'm new to AngularJS and attempting to redo some work on my previous application using the framework. I'm trying to create a Message Box service in my application (AngularJS v1.6.6) that modules call to display a pop-up in the UI.
I can't seem to figure out how I can bind the service's variables to the UI itself. I've tried creating a local 'MessageBox' variable in the controller and referencing its contents as well as simply calling the injected variable with no luck. Is there some way I can accomplish this through a service? I was thinking of a global variable but I've seen advice against this.
A service seemed like the best route to go but I'm open to any suggestions that make more sense here. The other modules need to display their own error or success messages so I thought it'd be easier to create a standard way to handle that.
My code is below:
message-box.template.html
<div id="message" ng-show="$ctrl.MessageBox.display" style="background-color: white; border: 1px solid black; margin: 5px; float: right; top: 60px; right: 2%; padding: 5px; position: fixed; z-index: 100;">
<img id="message-image" height="30" width="30" alt="{{ $ctrl.MessageBox.Image.alt }}" ng-src="{{ $ctrl.MessageBox.Image.source }}" /> <span id="message-text">{{ $ctrl.MessageBox.text }}</span>
</div>
message-box.module.js
angular.module("components.message-box", ["components"])
.controller("MessageBoxController", ["$scope", "MessageBox", function MessageBoxController($scope, MessageBox) {
}]);
message-box.component.js
/**
* message-box.component.js
* Contains the 'message-box' component
*/
angular.module("components.message-box")
.component("messageBox", {
templateUrl: "/Scripts/cop/components/message-box/message-box.template.html",
controller: "MessageBoxController"
});
message-box.service.js
/**
* message-box.service.js
* Defines the 'message-box' service
*/
angular.module("components.message-box")
.service("MessageBox", function () {
var self = this;
self.display = false;
self.Image = {
alt: null,
source: null
};
self.text = null;
self.timeout = null;
/**
* Hides the displayed message
* @public
*/
self.hideMessage = function () {
self.display = false;
self.timeout = null;
self.Image.alt = null;
self.Image.source = null;
self.text = null;
};
/**
* Displays the provided message to the user
* @public
* @param {string} text Message text
* @param {Object} Image Image display data
* @param {number} [hideTimeoutMs=null] Number of miliseconds to wait before hiding the message
*/
self.showMessage = function (text, Image, hideTimeoutMs) {
self.text = text;
self.Image.alt = Image.alt;
self.Image.source = Image.src;
self.display = true;
// Cancel timer for current timeout if it exists
if (self.timeout !== null) {
self.timeout = null;
}
// Set a new timeout if specified
if (hideTimeoutMs !== undefined) {
self.timeout = setTimeout(self.hideMessage, hideTimeoutMs);
}
};
});
Here's an example of attempting to call the service:
visual.module.js
angular.module("core.visual", [
"ngRoute",
"core.visual.settings"
])
.controller("VisualController", ["$routeParams", "$scope", "MessageBox",
function VisualController($routeParams, $scope, MessageBox) {
var self = this;
self.MessageBox = MessageBox;
self.MessageBox.showMessage("This is a message", "/Content/Images/loading.png", "Loading", 10000);
}
]);
I've also tried not using MessageBox as a local controller variable with no success - this is just my latest attempt.
Am I on the right track? Is this even possible?