$stateProvider
The $stateProvider is part of the ui-router library, which offers more advanced routing features compared to ngRoute. It allows for complex and nested routing configurations, enabling the creation of State-Based Routing Hierarchies. This is particularly useful for large and complex applications where routes can have multiple views and states. The $stateProvider provides a powerful and flexible way to manage different states of your application, making it a better choice when you need more advanced routing capabilities and state management.
Syntax
var app = angular.module('myApp', ['ui.router']); app.config(function ($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/defaultPath'); $stateProvider .state('stateName', { url: '/statePath', templateUrl: 'template.html', controller: 'ControllerName' }); });
Example: The below example demonstrates the usage of $stateProvider in AngularJS. Here, we have defined three states (‘dsa’, ‘fullstack’, and ‘ai’) and display content for each state in a <div ui-view> element when the user clicks on the respective images.
index.html
<!DOCTYPE html> < html ng-app = "myApp" > < head > < base href = "/" > < script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js" > </ script > < script src = "https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.22/angular-ui-router.min.js" > </ script > </ head > < body > < header style="background-color: #ffffff; text-align: center; padding: 20px 0;"> < h1 style="color: green; font-size: 40px; margin: 0;"> Welcome to w3wiki </ h1 > < h3 style="color: rgb(0, 0, 0); font-size: 34px;"> $stateProvider Example </ h3 > </ header > < nav style = "text-align: center;" > < div class = "image-link" ui-sref = "dsa" style="display: inline-block; margin: 20px; cursor: pointer; background-color: #fff; border-radius: 10px; transition: transform 0.2s;"> < img src = "https://media.w3wiki.org/img-practice/banner/dsa-to-development-coding-guide-thumbnail.png?v=19659" alt = "DSA" style="width: 300px; height: 200px; border: 3px solid #4CAF50; border-radius: 10px;"> < p style="color: rgb(0, 0, 0); font-size: 20px; padding: 10px;"> Data Structures and Algorithms </ p > </ div > < div class = "image-link" ui-sref = "fullstack" style="display: inline-block; margin: 20px; cursor: pointer; background-color: #fff; border-radius: 10px; transition: transform 0.2s;"> < img src = "https://media.w3wiki.org/img-practice/banner/full-stack-node-thumbnail.png?v=19659" alt = "Full Stack" style="width: 300px; height: 200px; border: 3px solid #4CAF50; border-radius: 10px;"> < p style="color: rgb(0, 0, 0); font-size: 20px; padding: 10px;"> Full Stack Development </ p > </ div > < div class = "image-link" ui-sref = "ai" style="display: inline-block; margin: 20px; cursor: pointer; background-color: #fff; border-radius: 10px; transition: transform 0.2s;"> < img src = "https://media.w3wiki.org/img-practice/banner/gate-data-science-and-artificial-intelligence-da-2024-thumbnail.png?v=19659" alt = "AI" style="width: 300px; height: 200px; border: 3px solid #4CAF50; border-radius: 10px;"> < p style="color: rgb(0, 0, 0); font-size: 20px; padding: 10px;"> Artificial Intelligence </ p > </ div > </ nav > < div ui-view class = "content" style="background-color: rgb(147, 251, 152); padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);"> </ div > < script > var app = angular.module("myApp", ["ui.router"]); app.config(function ($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/'); $stateProvider .state('dsa', { url: '/dsa', template: '< h1 >Data Structures and Algorithms</ h1 >'+ '< p >This program is designed to take you'+ ' on a transformative journey from mastering'+ ' Data Structures and Algorithms (DSA) to '+ 'becoming a proficient developer. Whether'+ 'you aspire to become a full-stack developer'+ ' or specialize in a specific technology stack, '+ 'this program provides the essential building '+ 'blocks for your coding journey starting right'+ ' from basic programming to building applications.</ p >' }) .state('fullstack', { url: '/fullstack', template: '< h1 >Full Stack Development</ h1 >'+ '< p >Looking to become a full-stack developer?;'+ ' This live, online course with a focus on the '+ 'popular JS library React for front-end and Node.js'+ ' for back-end along with APIs and deployment is'+ ' a must-have program for any aspiring developer.</ p >' }) .state('ai', { url: '/ai', template: '< h1 >Artificial Intelligence</ h1 >'+ '< p >Unlock success with our GATE Data Science'+ ' and Artificial Intelligence 2024. Specially '+ 'curated by experts, our courses in Machine Learning'+ ' and Artificial Intelligence are your gateway to'+ ' academic excellence. Fast-track your career and '+ 'unleash your potential - Enroll now!</ p >' }); }); </ script > </ body > </ html > |
Output:
What is the Difference Between $routeProvider and $stateProvider in AngularJS ?
In AngularJS, as we create Single-Page Applications, we need to implement the routing of components to view those images without having full page reloads. This can be achieved with the most popular routing methods, i.e., $routeProvider (ngRoute) and $stateProvider (ui-router).
In this article, we will explore the $routeProvider and $stateProvider in AngularJS and understand their basic implementation with the help of examples, along with knowing the difference between these two methods