Awais Saleem
Awais Saleem

Reputation: 71

How can i go to another page using angularjs

i am trying to pass data from one page in url and get in new page from the url, for example i want to add 10,20 in the url. like in the given example i pass 10,20 in sumurl it shows its sum in div ng-view but i want to show it in sumurl.html or input.html

var app=angular.module('myapp',['ngRoute']);
    app.config(['$routeProvider',function($routeProvider){
        
        $routeProvider
            .when('/sumurl/:a/:b',{
            templateUrl:'sumurl.html',
            controller:'sumurl'
        })
            .when('/input',{
            templateUrl:'input.html',
            controller:'input'
        })
            .when('/',{
            template:'Welcome to my app'
        }).otherwise({
            template:'Not Available'
        })
    }]);
    
    app.controller('sumurl',['$scope','$routeParams',function($scope,$routeParams){
        $scope.n1=$routeParams.a;
        $scope.n2=$routeParams.b;
        
    }]);
    
    app.controller('input',['$scope','$location','$interpolate',function($scope,$location,$interpolate){
        $scope.n1=0;
        $scope.n2=0;
        
        $scope.dosum=function(){
            var url=$interpolate('/sumurl/{{n1}}/{{n2}}')($scope);
                //console.log(url);
            $location.path(url);
        }
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<div ng-app="myapp">
<ul>
<li><a href="#/sumurl/10/20">some url</a></li>
<li><a href="#/input">input</a></li>
<li></li>
</ul>
    <div ng-view>
    
    </div>
    
</div>

Sumurl are and input are two html files

sumurl.html

<div>
    a = {{n1}}<br>
    b = {{n2}}<br>
    Sum = {{(n1-0)+(n2-0)}}
</div>

And input.html

<div>
    n1 = <input type="number" ng-model="n1"/>
    n2 = <input type="number" ng-model="n2"/>
    <button ng-click="dosum()">Sum</button>
    
</div>

Upvotes: 0

Views: 44

Answers (1)

Paul
Paul

Reputation: 36319

Inject the $location service, and then you can just do $location.href = <your target URL>

Upvotes: 1

Related Questions