Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I am trying to use the ng-click feature of AngularJS to switch views. How would I go about doing this with the code below?

Index.html

<div ng-controller="Cntrl"> 

 <div ng-click="someFunction()"> 

  click me 

 </div> 

</div>

The controller code:-

function Cntrl ($scope) {

 $scope.someFunction = function(){ 

//code to change view? 

} 

1 Answer

0 votes
by (106k points)

To switch between different views, you can directly change the window.location (using the $location service!) in index.html file as follows:-

<div ng-controller="Cntrl"> 

<div ng-click="changeView('edit')"> edit </div> 

<div ng-click="changeView('preview')"> preview </div> 

</div>

Controller.js

function Cntrl ($scope,$location) { 

$scope.changeView = function(view){

 $location.path(view); 

// path not hash 

}

Related questions

+1 vote
1 answer
+2 votes
1 answer
0 votes
1 answer

Browse Categories

...