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

前端 未结 8 596
爱一瞬间的悲伤
爱一瞬间的悲伤 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 16:54

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

    <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
            }
        }
    

    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 )

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

    The provided answer is absolutely correct, but I wanted to expand for any future visitors who may want to do it a bit more dynamically -

    In the view -

    <div ng-repeat="person in persons">
        <div ng-click="changeView(person)">
            Go to edit
        <div>
    <div>
    

    In the controller -

    $scope.changeView = function(person){
        var earl = '/editperson/' + person.id;
        $location.path(earl);
    }
    

    Same basic concept as the accepted answer, just adding some dynamic content to it to improve a bit. If the accepted answer wants to add this I will delete my answer.

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

    This little function has served me well:

        //goto view:
        //useage -  $scope.gotoView("your/path/here", boolean_open_in_new_window)
        $scope.gotoView = function (st_view, is_newWindow)
        {
    
            console.log('going to view: ' + '#/' + st_view, $window.location);
            if (is_newWindow)
            {
                $window.open($window.location.origin + $window.location.pathname + '' + '#/' + st_view, '_blank');
            }
            else
            {
                $window.location.hash = '#/' + st_view;
            }
    
    
        };
    

    You dont need the full path, just the view you are switching to

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

    I've got an example working.

    Here's how my doc looks:

    <html>
    <head>
        <link rel="stylesheet" href="css/main.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-resource.min.js"></script>
        <script src="js/app.js"></script>
        <script src="controllers/ctrls.js"></script>
    </head>
    <body ng-app="app">
        <div id="contnr">
            <ng-view></ng-view>
        </div>
    </body>
    </html>
    

    Here's what my partial looks like:

    <div id="welcome" ng-controller="Index">
        <b>Welcome! Please Login!</b>
        <form ng-submit="auth()">
            <input class="input login username" type="text" placeholder="username" /><br>
            <input class="input login password" type="password" placeholder="password" /><br>
            <input class="input login submit" type="submit" placeholder="login!" />
        </form>
    </div>
    

    Here's what my Ctrl looks like:

    app.controller('Index', function($scope, $routeParams, $location){
        $scope.auth = function(){
            $location.url('/map');
        };
    });
    

    app is my module:

    var app = angular.module('app', ['ngResource']).config(function($routeProvider)...
    

    Hope this is helpful!

    0 讨论(0)
  • 2020-11-30 17:11
    Firstly you have to create state in app.js as below
    
    .state('login', {
          url: '/',
          templateUrl: 'views/login.html',
          controller: 'LoginCtrl'
        })
    
    and use below code in controller
    
     $location.path('login'); 
    

    Hope this will help you

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

    There are two ways for this:

    If you are using ui-router or $stateProvider, do it as:

    $state.go('stateName'); //remember to add $state service in the controller
    

    if you are using angular-router or $routeProvider, do it as:

    $location.path('routeName'); //similarily include $location service in your controller
    
    0 讨论(0)
提交回复
热议问题