简体   繁体   English

angularjs路由更改控制器

[英]angularjs route change controller

I want to use Angular-route to change the controller used in a page 我想使用Angular-route来更改页面中使用的控制器
This is some part of my angular code 这是我的角度代码的一部分

var cat_list = angular.module('getCat', ['ngRoute']);

cat_list.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when('category/upcoming', {
       controller: 'getUpcoming'
    })
    .when('category/popular', {
        controller: 'getPopular'
    });

    $locationProvider.html5Mode(true);
});

cat_list.controller('getUpcoming', ['$scope', function($scope, $routeParams) {
//controller code goes here
});
cat_list.controller('getPopular', ['$scope', function($scope, $routeParams) {
//controller code goes here
});

and this is some part of my html 这是我的HTML的一部分

<div class="row" ng-app="getCat">
    <div class="col-md-12">
        <!-- something happen in here -->           
    </div>
</div>

but when I tried to visit http://localhost/project/category/upcoming or http://localhost/project/category/popular the route is not working (no controller picked or selected) 但是当我尝试访问http://localhost/project/category/upcominghttp://localhost/project/category/popular ,路由不起作用(未选择或选择控制器)

did I miss something? 我错过了什么?

Try this 尝试这个

var cat_list = angular.module('getCat', ['ngRoute']);

cat_list.config(['$routeProvider', '$locationProvider', 

etc etc. 等等等

Could you also please explain why you want to use different controllers on the same page? 您能否同时解释一下为什么要在同一页面上使用不同的控制器? Why dont you use two different partials to create two seperate pages for upcoming and popular? 您为什么不使用两个不同的部分来为即将到来的流行页面创建两个单独的页面?

Or, a different approach would be to just use the same controller, and filter your results using the appropriate directive, without changing the stuff that you load each time. 或者,另一种方法是只使用相同的控制器,并使用适当的指令过滤结果,而不更改每次加载的内容。

Have to mention ng-view, so that this directive is the placeholder to replace your view partials on url changes. 必须提及ng-view,以便该指令是占位符,用于替换url更改时的视图部分。

<div class="row" ng-app="getCat">
    <div class="col-md-12">
        <div ng-view></div> // ---- This is the thing you missed
    </div>
</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM