1

How to redirect to other url when I click on a div using angularjs.can anyone please help me out ...

My html:

 <div ng-click="redirectTo()">
    <p>Hello</p>
    </div>

My js:



  angular.module('myApp')
   .controller('MainCtrl', ['$scope', '$rootScope',
                            function ($scope,$rootScope) {

      $scope.redirectTo = function(){

        ===============

        }
}]);

2 Answers 2

2

If you are using states then use :

<div data-ui-sref="// your state" >
  <p>Hello</p>
</div>

Or if you want to use on-click event then use :

angular.module('myApp')
.controller('MainCtrl', ['$scope', '$state',
                        function ($scope,$state) {
 $scope.redirectTo = function(){
     $state.go('// you state',null);
 }
    }
}]);

HTML TEMPLATE

<div ng-click="redirectTo()">
   <p>Hello</p>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for ur help
You should go through this ui-sref
1
  angular.module('myApp')
   .controller('MainCtrl', ['$scope', '$rootScope','$location',
                            function ($scope,$rootScope,$location) {

      $scope.redirectTo = function(){

         $location.url('/home');

        }
}]);    

5 Comments

Here where should I use $location in the controller
Inside controller you can use. you need to inject $location in controller. .controller('MainCtrl', ['$scope', '$rootScope','$location', function ($scope,$rootScope, $location)
it Working? if yes means can you tick my answer.
yes its working.If it is home,then how should I redirect to templateURL using route provider
{ "Home": { "url": "/Home", "templateUrl": "Home.html" }, "Page1": { "url": "/page1", "templateUrl": "Page1.html" }, "Page2": { "url": "/page2", "templateUrl": "page2.html" }, "Page3": { "url": "/page3", "templateUrl": "Page3.html" } } Sample provider whatever url your set it will call corresponding templateurl based on that page will load

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.