简体   繁体   English

AngularJS:如何从控制器功能切换视图?

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

I am trying to use the ng-click feature of AngularJS to switch views. 我正在尝试使用AngularJS的ng-click功能来切换视图。 How would I go about doing this with the code below? 我将如何使用下面的代码执行此操作?

index.html 的index.html

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

controller.js controller.js

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

In order to switch between different views, you could directly change the window.location (using the $location service!) in index.html file 为了在不同的视图之间切换,您可以直接在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 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 ). 并配置路由器根据位置切换到不同的部分(如https://github.com/angular/angular-seed/blob/master/app/app.js所示)。 This would have the benefit of history as well as using ng-view. 这将有历史的好处以及使用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 ) 或者,你使用ng-include和不同的部分,然后使用ng-switch,如下所示( https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html

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. 如果接受的答案想要添加这个,我将删除我的答案。

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: 这是我的Ctrl看起来像:

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

app is my module: app是我的模块:

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

Hope this is helpful! 希望这有用!

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. 用于此问题的所有先前答案的方法建议更改不必要的URL,并且我认为读者应该知道替代解决方案。 I use ui-router and $stateProvider to associate a state value with a templateUrl which points to the html file for your view. 我使用ui-router和$ stateProvider将状态值与templateUrl相关联,templateUrl指向视图的html文件。 Then it is just a matter of injecting the $state into your controller and calling $state.go('state-value') to update your view. 然后,只需将$ state注入您的控制器并调用$ state.go('state-value')来更新您的视图。

What is the difference between angular-route and angular-ui-router? angular-route和angular-ui-router之间有什么区别?

There are two ways for this: 有两种方法:

If you are using ui-router or $stateProvider , do it as: 如果您使用的是ui-router或$stateProvider ,请执行以下操作:

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

if you are using angular-router or $routeProvider , do it as: 如果您使用angular-router或$routeProvider ,请执行以下操作:

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

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. 如果不对默认路由(#/ ViewName)环境进行全面改造,我就可以稍微修改一下Cody的提示并使其工作得很好。

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. 带我到这个解决方案的是当我试图将一个Kendo Mobile UI小部件集成到一个角度环境中时,我正在丢失我的控制器的上下文,并且常规锚标记的行为也被劫持。 I re-established my context from within the Kendo widget and needed to use a method to navigate...this worked. 我从Kendo小部件中重新建立了我的上下文,并且需要使用方法来导航......这很有效。

Thanks for the previous posts! 感谢以前的帖子!

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 希望对你有帮助

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 您不需要完整路径,只需要切换到的视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从指令AngularJs访问控制器功能? - How do I access controller function from directive AngularJs? 如何在AngularJS上从控制器调用服务上的函数 - How do i call a function on a Service from a Controller, on AngularJS 从AngularJS控制器,如何解析模块中定义的另一个控制器功能(动态ng-controller)? - From an AngularJS controller, how do I resolve another controller function defined in the module (dynamic ng-controller)? 如何在Angularjs中使用超时功能切换div图层? - How do I switch div layers with a timeout function in Angularjs? 如何从主干控制器执行 angularJS 控制器中的函数 - How can I execute a function in an angularJS controller from a backbone controller 如何在Angular中切换视图? - How do I switch views in Angular? 如何从angularjs中的控制器CandidateCtrl.js访问Data.js的功能? - how do I access function of Data.js from a controller CandidateCtrl.js in angularjs? AngularJS:如何从控制器调用在指令范围内定义的函数? - AngularJS: How do I call a function defined in a directive's scope from a controller? AngularJS / UI-Router:如何将一个控制器与多个命名视图一起使用? - AngularJS/UI-Router: How do I use one controller with multiple named views? 如何从javascript函数获取ID到angularJS控制器 - How can I get the ID from a javascript function to an angularJS controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM