Is there anyway to access the GET variable in angularJS - angularjs

How can I access the GET variable of the current PHP script that the AngularJS script is bootstrapped to?
I read that the $window service will give window-bounded variables to the controller but I don't know how to access these. Is the GET variable even in this?

Without including ngRoute in your project you can use the $location service.
Inject the $location service into your controller (I'm assuming you know what this means as you apparently did it with the $window service)
You can then access the get variables with
$location.search().user_id;
Hope this helps, Good luck!

Related

How to use Angular factory data throughout the project

I have an Angular js factory. In which I am storing the user information after successful login. Now I have multiple controllers in all of them I need this user information so I need to inject this factory to each and every controller. Is there any way to access this factory data throughout the project without injecting in each and every controller?
Can we directly inject it into the module?
Some suggestions :
storing the user information in factory is not a good approach because if user reload the page it will reset the factory data and user will not exist anymore.
As sajeetharan suggested, you can use HTML5 localStorage or sessionStorage to store the user information. So, that without injecting you can easily access stored storage value everywhere you want.
Yes, if you need the user information so you need to inject the factory to each and every controller because on switching from one controller to another controller you have to fetch the data from the factory.If you inject the factory only in the main module it will not load the factory when you move from success login to some other page.
User information can be stored in local storage or cookie so that i can be accessed anywhere.
You can also consider using $rootScope, in that case also you need to inject in all the modules which you require.
a factory is part of a module.
Injecting it into the controllers needed is the way of using the saved values throughout the module.

Once I turn off debuggingData in angular, is there another way to get the rootscope?

I think having debugging info on by default in Angular will sink large apps. You can now disable this in production:
https://docs.angularjs.org/guide/production#disabling-debug-data
But there are some cases where I do:
angular.element(document).scope()
in order to access the rootScope. Is there another way in angular to get the $rootScope without this debugging feature on?
To access $rootScope you could do
angular.element(document).injector().get('$rootScope')

ANGULAR runtime add service to module

How can i add a service to the module/angular app at runtime.
Will $injector or $provide help me?
Something like
app.addService('s',['k',function(k){console.log('new Service k');}]);
Yes, $provide will do it. See the documentation at https://docs.angularjs.org/api/auto/service/$provide
So you could do something like this (from within foo):
app.factory('foo',functon() {
$provide.factory('s',function() {
console.log("new service 's'");
});
});
The problem is that you cannot inject $provide directly into the service, so you need to capture it at module creation time.
The question is, why would you want to do this? Since the service 's' isn't declared until 'foo' is run, you run the risk of calling the service as a dependency at some other point. If it is anywhere within a normal injection, you would probably get some weird error.
Matter of fact, I think the only way it would work is by using the $injector to get it from within some other service.
So, yes, you could do it, but would you really want to?
Look at this fiddle: http://jsfiddle.net/ch8vu4ng/1/

Refresh a controller from another Controller ~ Angularjs

Is there a way to refresh a controller from another controller?
For example, Controller1 runs then Controller2 runs. At the end of Controller2 there is a
"command" to re-run Controller1. Is this possible?
In my post I had made earlier it seems like my questions was unclear what I was trying to do. Here is a link to it.
Updating a controller from another controller Angular
if you are using ngRoute there is a method to re-run the all the controller without having to reload the page.
app.controller('MyCtrl', function($route){
$route.reload()
})
If you want to refresh data, please use services and refresh data in the services. Sequential things can be done of ajax by using promises. Please let me know if you need any help in designing the solution
I think you need to look into using directives. By using directives you can encapsulate and share scope objects.
Please read the documentation on directives and "shared scope".
http://docs.angularjs.org/guide/directive

Resolving a service programmaticallyin AngularJs

I am trying to resolve a service programmatically in angular js from a particular module, but I can't find the correct way to do that.
I know using angular.injector(['NameOfModule']).get('NameOfService') I can retrieve the service. However, using angular.injector does not retrieve the injector of the given module and instead creates a new injector. This means that when it retrieves the service, as it is a new injector, it does not have the service cached and so creates a new one. This means that I will have several of the same service, and so it will no longer be a singleton, which will obviously cause problems.
What I essentially want to do is retrieve the injector from a particular module so I can try to retrieve the (cached or not cached) service from that injector. Does anyone know how to achieve this?
What i would suggest would be to inject the $injector service into the controller\service where you want to use it. This would return the injector that the app is using and not create a new injector as angular.injector does. See detailed explanation in this SO post Can't retrieve the injector from angular

Resources