Sunday 21 August 2016

javascript - AngularJS : How do I switch views from a controller function?



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



 


click me





controller.js



  function Cntrl ($scope) {
$scope.someFunction = function(){
//code to change view?
}
}


Answer



In order to switch between different views, you could directly change the window.location (using the $location service!) in
index.html file





edit



preview




Controller.js



function Cntrl ($scope,$location) {
$scope.changeView = function(view){
$location.path(view); // path not hash

}
}


and configure the router to switch to different partials based on the location ( as shown here https://github.com/angular/angular-seed/blob/master/app/app.js ). This would have the benefit of history as well as using ng-view.



Alternatively, you use ng-include with different partials and then use a ng-switch as shown in here ( https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html )


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...