Hi Aidan,
In this case, I would only use a directive if you want to either style your messages in a way that goes beyond CSS, or show messages in more than one place on your page.
Instead, I would try creating a simple service that includes the following methods:
- Push a message to a stack (string array).
- Since I can have concurrent operations with messages, but only want to show one at a time, an array of messages is necessary. Note that we will only show one at a time, however.
- This is usually called when you do your server post.
- Push a timed message to the stack.
- A message like "Saved!" should linger for about five seconds, then swap out for a default message, or blank.
- Be sure to use Angular's $timeout service for this.
- This is usually called in your AJAX callback.
- "Cancel" a message in the stack.
- Once an operation is done, I typically want to remove one message ("Saving customer data...") while pushing a new one ("Saved!").
- This is usually called in your AJAX callback.
- Get the message at the top of the stack.
- This will be consumed by the controller that shows your message.
Once your service is working, hook it up to the controllers and services that will be pushing and cancelling messages. They will call the first three methods.
Finally, create another controller for the area on your page where you want to show the message. Give the controller's model a method that exposes the service's get method, and then ng-bind that method to an element in your markup.
I realize that description is a little abstract; hope it helps, anyhow!