cgross / angular-notify

Minimalistic and extensible notification service for Angular.

Home Page:http://cgross.github.io/angular-notify/demo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cdep on $http for global notifications

davidhantson opened this issue · comments

Hi, I really like the simplicity of this module and I tried implementing it today.

I thought the perfect case for these notifications would be global Ajax message handling, but the interceptors are dependencies of the $http module. Apparently the cgNotify module also uses $http as a dependency. As a consequence there is an angular cdep (circular dependency) error.

Any suggestions on how to use your module for global message handling?

Check console for cdep error:
http://plnkr.co/edit/Wlnl6wdOsC5pDVZRgZgs?p=preview
Same code working with notify dependency in comment for the interceptor:
http://plnkr.co/edit/S7v9Lcon7SGkGVjDZD8d?p=preview

In cases like this, where A needs B and B needs A and but A doesn't need B until after its been initialized, you can just the $injector service directly to get a service dynamically. So instead of injecting notify into the factory() call, instead inject $injector. Then call $injector.get('notify') to get the instance of notify whenever you need it. Like:

var notify = $injector.get('notify');
notify('success');

That said, you'll run into problems because notify loads its template via $http and that will in turn trigger the request interceptor (that will in turn trigger notify to use $http, triggering your interceptor, rinse repeat). You'll need to filter out some calls from your interceptor to make it work.

Perfect!! Thx a billion.

There is indeed a filter. When my response from node contains a message property It will be shown.

{
  message: "Something went wrong"
}

{
  message: "Successfully saved"
}