Redirecting in AngularJS using UI Router

I attempted to perform a page redirection using angular redirect, but encountered a "couldn't resolve from state" error. I referenced the guide on passing parameters while redirecting in AngularJS using UI Router for assistance.

Below is my route.js code:

var helloWorldApp = angular.module('helloWorldApp', ['ui.router']);
helloWorldApp.config(function($stateProvider,$urlRouterProvider) {
$stateProvider
.state('first', {
    url: '/first',
    templateUrl: 'first.html'
})

.state('second', {
    url: '/second',
    templateUrl: 'second.html'
});
});

Content of my first.html:

<html lang="en" ng-app="helloWorldApp">
<head>
<meta charset="UTF-8">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.2.js"></script>
<script type="text/javascript" src="./app/routes.js"></script>
</head>
<script type="text/javascript">
var helloAppConf = angular.module('helloWorldApp', ['ui.router']);
helloAppConf.controller('firstCtrl',function($scope,$state){
    $scope.userInput='Hello world from first page';
    $scope.getNewPage=function() {
        $state.go("second", { id: $scope.userInput });
    }
});

</script>
<body>
    <div ng-controller="firstCtrl">
        <h4>{{userInput}}</h4>
        <button ng-click="getNewPage()">New Page</button>
    </div>
</body>
</html>

Content of my second.html:

<!DOCTYPE html>
<html lang="en" ng-app="helloWorldApp">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.2.js"></script>
    <script type="text/javascript" src="./app/routes.js"></script>
</head>
<script type="text/javascript">
    var helloAppConf = angular.module('helloWorldApp', ['ui.router']);
        helloAppConf.controller('secondCtrl' , function($scope, $state){
        $scope.id = $stateParams.id;
    });
</script>
<body>
<div ng-controller="secondCtrl">
    <h4>{{id}}</h4>
    <button ng-click="getNewPage()">New Page</button>
</div>
</body>
</html>

I'm utilizing springboot and you can view a snapshot of my project structure here: https://i.sstatic.net/JYhYk.png

While I can access both first.html and second.html pages through my browser, clicking on the New Page button within first.html results in the following error message: Error: Could not resolve 'second' from state '' at Object.t.transitionTo (http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js:7:8834) at Object.t.go (http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js:7:8182) at Scope.$scope.getNewPage (http://localhost:8080/first.html:18:16) at fn (eval at (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js:13322:15), :4:221) at callback (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js:23549:17) at Scope.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js:15989:28) at Scope.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js:16089:25) at HTMLButtonElement. (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js:23554:23) at HTMLButtonElement.eventHandler (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js:3298:21)

I would greatly appreciate any assistance. Thank you in advance.

Answer №1

The problem in your second block of html code lies in the incorrect injection of $state instead of $stateParams, causing your code to malfunction.

To resolve this issue, make sure to inject using $stateParams.

var helloAppConf = angular.module('helloWorldApp', ['ui.router']);
    helloAppConf.controller('secondCtrl' , function($scope, $stateParams){
    $scope.id = $stateParams.id;
});

Alternatively, you can access the id parameter using $state.params.id.

var helloAppConf = angular.module('helloWorldApp', ['ui.router']);
    helloAppConf.controller('secondCtrl' , function($scope, $state){
    $scope.id = $state.params.id;
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In order to ensure that the multiselect bootstrap dropdown options drop outside of the div and prevent scroll bubbling, there are specific

I am trying to customize a bootstrap multiselect dropdown located within a div. You can find an example of what I'm working on at this link: http://jsfiddle.net/The_Outsider/8watL2L1/9/ In my design, I want the multiselect dropdown options to drop ou ...

Updating marginheight to "0" for iframe using JavaScript

While attempting to validate the code on my webpage, I encountered an error stating that the "marginheight" and "marginwidth" attributes of my iframe should be set in CSS instead. Through research, it appears that this cannot be achieved directly through C ...

Activate Mixitup Filters Automatically When Page Loads

I have integrated the Mixitup utility from into my ExpressionEngine website. The purpose of this integration is to filter a listing of real estate properties by region, features, and other criteria. My goal is to allow users to pass values via URL segmen ...

Enhanced efficiency in the interaction between front-end JavaScript and back-end JavaScript

As a frontend developer, I find myself in the unique position of working on a project that involves processing a large amount of data in my nodeJS backend (while using reactJS for the front end). After the necessary data processing is completed in the bac ...

Creating a soft focus effect in a specific region (behind the text)

While working on my homepage created with HTML/CSS/Javascript, I have text positioned at the top left corner of the screen. The challenge arises from the fact that I am using different backgrounds sourced from Reddit, and a script randomly selects one duri ...

Exploring ways to incorporate whitespace and newline characters in the split function

Looking for assistance with iterating through a string that contains multiple words separated by spaces and new lines (\n). Here's an example: message: dob cat dog dog cat cat I tried using the following function to split the mes ...

Utilizing an external type definition in JSDoc @typedef

I'm encountering an issue with reducing redundancy when defining my imported types. I am trying to streamline the process, but unfortunately I am running into errors. /** @typedef {import("../types/util")} util @typedef {util.mapBehaviors} m ...

Obtaining slider images using XML in ColdFusion

I've been attempting to incorporate a slider into my ColdFusion page by using a jQuery script that fetches images from an XML file. The XML file contains paths and other image details, but unfortunately, the slider isn't functioning as expected. ...

What function does an API fulfill within the MVC architecture?

My quest to fully grasp MVC continues. I understand that in a traditional setup, the controller processes user requests for a webpage. For example, if a user wants to see 5 blogposts, the controller instructs the Model to fetch those posts from the databa ...

Steps for triggering a logout using MSAL2Provider

Currently, I am incorporating the React Login Microsoft-Graph-Toolkit component (shown in the code snippet below) along with MSAL2Provider to facilitate user login functionality in my Active Directory application. Although this setup is functioning smoothl ...

What is the best way to stack animations in CSS and HTML?

I need help figuring out how to layer two different animations on my website. Specifically, I want to create an effect where twinkling stars are in the background with a moving moon animation layered on top of them. However, when I try to implement this, t ...

What techniques can be employed to utilize multiple JavaScript files?

Hey there, I am facing an issue while trying to execute multiple JavaScript codes. My first script is running smoothly with the change function, but the second one seems to be causing some trouble. Can anyone guide me on how to effectively run multiple J ...

Tips for avoiding sending watermark text when a button is pressed

I have integrated a feature that adds watermark text to some of the input boxes in my project. You can find more information about how I implemented this functionality here: While this feature is helpful, it has presented me with an issue. When I submit a ...

Retrieve the value of the element and combine them together (The parseFloat function may not work as expected

The following code is what I am currently working with: var num1=$("#num1").val(); //the num1 value is 12.60 //html code for this number <input id="num1" value="12.60" /> num1=parseFloat(num1).toFixed(2); var num2=$("#num2").text(); //the num2 valu ...

What is the proper way to display the date and time 2021-11-14T18:30:00.000+00:00?

Here is my ts file code: mydate: Date = new Date('2021-11-14T18:30:00.000+00:00'); However, I want the date to be in this format:- 07-July-2022 If anyone can assist with achieving this format, it would be greatly appreciated. Thank you! ...

How can I use JavaScript to optimize and cache an HTML video element when the streaming size exceeds the file size?

Currently, I have successfully set up a video streaming feature using a NodeJS server. The issue I am facing is related to the video size, which is 61MB. Whenever I adjust the range in the HTML video element, it triggers another fetch request, leading to a ...

Can Javascript be used to open a new window, send POST parameters to it, and pass the request as application/json?

I have a browser client application built with JavaScript that manages a large amount of state data. Sometimes, this data needs to be organized into a structured object and sent to a new window for further processing. Currently, I am using a solution wher ...

"Troubleshooting issue in Django 1.6.2 where CSS styles are not being applied to

I am encountering an issue with this code and would greatly appreciate some assistance. The base.html file contains a {% block content %}{% endblock %}. I have created a Signup.html file structured like this: {%extends 'base.html'%} {% block co ...

Utilizing d3 for Nested jQuery Accordions

I'm facing a challenging issue trying to nest jQuery accordions using d3. The difficulty arises from the complexity of creating HTML structures like the one below with nested accordions, and utilizing d3's nest and data binding functionalities: ...

Struggling to retrieve integer values from a JSON object array

I'm currently working on a function to iterate through an array of objects retrieved from the server. When I console.log the response, this is the structure I see. https://i.sstatic.net/ErOps.png This is the JavaScript function I've created for ...