0

I am trying to redirect to another page using angular redirect. But I am getting a couldn't resolve from state error. I followed the How to pass parameter when I redirect the page in angularjs using ui router? link.

Please find below 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'
});
});

My first.html code

<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>

My second.html code

<!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 am using springboot and my project structure snapshot is attached here: Project Structure

I am able to access both first.html and second.html pages on my browser. But when I click on the New Page button on first.html, I get the 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)

Please help. Thanks in advance.

1
  • Why two html pages? You should have one index.html file and manage it states with <ui-view>. So I'm not sure what you want to do here. Commented Oct 29, 2015 at 6:41

1 Answer 1

0

In your second html code you inject $state wrongly instead of $stateParams. So that your code didn't work

Inject with $stateParams

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

or use $state.params.id

var helloAppConf = angular.module('helloWorldApp', ['ui.router']);
    helloAppConf.controller('secondCtrl' , function($scope, $state){
    $scope.id = $state.params.id;
});
Sign up to request clarification or add additional context in comments.

1 Comment

I tried both your suggestions. They didn't work. I am beginning to think if its a path issue and my JS is not able to find the html templates? Do I have to add additional tags or define them as ng-templates in my html so that routes.js can see it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.