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

前端 未结 8 597
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 16:49

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

 

        
相关标签:
8条回答
  • 2020-11-30 17:16

    The method used for all previous answers to this question suggest changing the url which is not necessary, and I think readers should be aware of an alternative solution. I use ui-router and $stateProvider to associate a state value with a templateUrl which points to the html file for your view. Then it is just a matter of injecting the $state into your controller and calling $state.go('state-value') to update your view.

    What is the difference between angular-route and angular-ui-router?

    0 讨论(0)
  • 2020-11-30 17:16

    Without doing a full revamp of the default routing (#/ViewName) environment, I was able to do a slight modification of Cody's tip and got it working great.

    the controller

    .controller('GeneralCtrl', ['$route', '$routeParams', '$location',
            function($route, $routeParams, $location) {
                ...
                this.goToView = function(viewName){
                    $location.url('/' + viewName);
                }
            }]
        );
    

    the view

    ...
    <li ng-click="general.goToView('Home')">HOME</li>
    ...
    

    What brought me to this solution was when I was attempting to integrate a Kendo Mobile UI widget into an angular environment I was losing the context of my controller and the behavior of the regular anchor tag was also being hijacked. I re-established my context from within the Kendo widget and needed to use a method to navigate...this worked.

    Thanks for the previous posts!

    0 讨论(0)
提交回复
热议问题