簡體   English   中英

AngularJS:如何從控制器功能切換視圖?

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

我正在嘗試使用AngularJS的ng-click功能來切換視圖。 我將如何使用下面的代碼執行此操作?

的index.html

 <div ng-controller="Cntrl">
        <div ng-click="someFunction()">
            click me
        <div>
    <div>

controller.js

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

為了在不同的視圖之間切換,您可以直接在index.html文件中更改window.location(使用$ location服務!)

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

並配置路由器根據位置切換到不同的部分(如https://github.com/angular/angular-seed/blob/master/app/app.js所示)。 這將有歷史的好處以及使用ng-view。

或者,你使用ng-include和不同的部分,然后使用ng-switch,如下所示( https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html

提供的答案是絕對正確的,但我希望擴展可能想要更動態地做任何未來的訪問者 -

在視圖中 -

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

在控制器中 -

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

與接受的答案相同的基本概念,只是添加一些動態內容以改善一點。 如果接受的答案想要添加這個,我將刪除我的答案。

我有一個工作的例子。

這是我的文檔的樣子:

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

這是我的部分看起來像:

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

這是我的Ctrl看起來像:

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

app是我的模塊:

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

希望這有用!

用於此問題的所有先前答案的方法建議更改不必要的URL,並且我認為讀者應該知道替代解決方案。 我使用ui-router和$ stateProvider將狀態值與templateUrl相關聯,templateUrl指向視圖的html文件。 然后,只需將$ state注入您的控制器並調用$ state.go('state-value')來更新您的視圖。

angular-route和angular-ui-router之間有什么區別?

有兩種方法:

如果您使用的是ui-router或$stateProvider ,請執行以下操作:

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

如果您使用angular-router或$routeProvider ,請執行以下操作:

$location.path('routeName'); //similarily include $location service in your controller

如果不對默認路由(#/ ViewName)環境進行全面改造,我就可以稍微修改一下Cody的提示並使其工作得很好。

控制器

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

風景

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

帶我到這個解決方案的是當我試圖將一個Kendo Mobile UI小部件集成到一個角度環境中時,我正在丟失我的控制器的上下文,並且常規錨標記的行為也被劫持。 我從Kendo小部件中重新建立了我的上下文,並且需要使用方法來導航......這很有效。

感謝以前的帖子!

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'); 

希望對你有幫助

這個小功能對我有好處:

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


    };

您不需要完整路徑,只需要切換到的視圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM