Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  64] [ 7]  / answers: 1 / hits: 21712  / 11 Years ago, thu, march 7, 2013, 12:00:00

I need to register a method available everywhere in angularjs. This method has 2 arguments (the resource id, the callback on deletion success) and it uses the resource provider to actually delete the item.



Then to register it, I need that angularjs injects me the $rootScope and MyResourceProvider. My first idea was to do that in my home page controller:




var HomeCtrl = function ($rootScope, MyResourceProvider) {
$rootScope.confirmAndDeletePackage = function (sId, fCallback) {
// do some stuff
MyResourceProvider.delete({id: sId}, fCallback);
}
}


Here starts actually my issue. That works fine in a regular navigation (home -> list -> select -> delete) but if the user accesses directly a page where the delete button is available w/o passing through the home page, this method will not be available (because the HomeController has not been initialized)...



So, my question is where can I move this piece of code to ensure it will always be executed at the application bootstrap.



I tried on myApp.config() but w/o success...



Any idea?


More From » jquery

 Answers
1

As @ganaraj mentioned in the comments, a service is probably a better choice for this.



However, to answer your question, you can use the run() method.



myApp.run(function($rootScope, MyResourceProvider) {
$rootScope.confirmAndDeletePackage = function (sId, fCallback) {
// do some stuff
MyResourceProvider.delete({id: sId}, fCallback);
}
})


run() is called after all modules have been loaded.


[#79768] Wednesday, March 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taliac

Total Points: 84
Total Questions: 114
Total Answers: 114

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;