Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $state and $stateProvider?

I'm currently reading through an AngularJS 1.4.8-based application that uses AngularUI Router 0.2.15.

I noticed that this application sometimes uses a dependency $state and sometimes a dependency $stateProvider.

Are there any differences between injecting and working with $state and $stateProvider?

like image 897
Abdull Avatar asked Jul 29 '16 09:07

Abdull


People also ask

What is stateProvider state?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.

What is UrlRouterProvider otherwise?

otherwise(rule: string | RawNg1RuleFunction): UrlRouterProvider. Defines the path or behavior to use when no url can be matched.

What is UI-router?

UI-Router is the defacto standard for routing in AngularJS. Influenced by the core angular router $route and the Ember Router, UI-Router has become the standard choice for routing non-trivial apps in AngularJS (1. x).

What is stateParams in AngularJS?

What Is the $stateParams Service in Angular? $stateParams is a service that captures URL-based parameters, and we can use these parameters to display information according to the state. Let's create an example with two states, understand how states work, and use $stateparams to store parameters.


1 Answers

There are two types of singleton instances in angularjs.

One is the service instance we are always using, like $state.

The other one is called the 'provider', like $stateProvider.

'Providers' hold the factory function for the common service and is responsible for creating the corresponding service.

In other word, $stateProvider creates $state.

From the angular documentation:

An Angular service is a singleton object created by a service factory. These service factories are functions which, in turn, are created by a service provider. The service providers are constructor functions. When instantiated they must contain a property called $get, which holds the service factory function.

When angular is instantiating a module, it will first create the providers, and after that it will run the 'module.config' functions.

However, services like $state are not instantiated by that time. They will be instantiated by their providers when needed (e.g. injected into other services) after 'module.config' is finished. So u cannot put a service in a module.config.

Both module.service and module.factory are creating providers in fact.They are shortcuts so we can always create services easily.

like image 142
MMhunter Avatar answered Nov 28 '22 03:11

MMhunter