根据路线切换ng-include的可见性

时间:2013-02-28 02:28:54

标签: javascript angularjs angularjs-routing

我有以下配置:

$routeProvider
.when('/cars', { templateUrl: 'app/cars/index.html', controller: 'CarsCtrl', reloadOnSearch: false })
.when('/bikes', { templateUrl: 'app/bikes/index.html', controller: 'BikesCtrl', reloadOnSearch: false });

在我的根index.html的某个地方有一个:

<a href="#/cars">Cars</a>
<a href="#/bikes">Bikes</a>
<div ng-view></div>

现在,我希望同时在DOM中加载和生成两个视图 ,并根据路径/ URL显示其中一个。

类似下面的内容(不是实际的工作代码,只是为了给你一个想法)。

app.js:

$routeProvider
.when('/cars', { controller: 'CarsCtrl', reloadOnSearch: false })
.when('/bikes', { controller: 'BikesCtrl', reloadOnSearch: false });

root index.html:

<a href="#/cars">Cars</a>
<a href="#/bikes">Bikes</a>
<div ng-include="'app/cars/index.html'" ng-show="carsVisible"></div>
<div ng-include="'app/bikes/index.html'" ng-show="bikesVisible"></div>

更新:我知道ng-view类型可以做到这一点,但存在细微差别。我希望每个视图的html生成一次并始终保留在DOM中。

4 个答案:

答案 0 :(得分:9)

我创建了一个RouteCtrl来通过ng-include加载你的所有视图。没有使用ng-view。我内联了模板。模板可以包含自己的ng-controller指令以引入特定的控制器。

<body ng-controller="RouteCtrl">
  <a href="#/cars">Cars</a>
  <a href="#/bikes">Bikes</a>
  <div ng-controller="RouteCtrl">
      <div ng-include="'/cars.html'"  ng-show="carsVisible"></div>
      <div ng-include="'/bikes.html'" ng-show="bikesVisible"></div>
  </div>

  <script type="text/ng-template" id="/cars.html">
     Cars template.
  </script> 
  <script type="text/ng-template" id="/bikes.html">
     Bikes template.
  </script> 

$ routeProvider仍然配置,但没有指定模板或控制器,导致RouteCtrl始终处于活动状态。该控制器侦听$routeChangeSuccess事件并相应地操作ng-show属性。

app.config(function($routeProvider) {
  $routeProvider
     .when('/cars', {} )
     .when('/bikes', {})
});

app.controller('RouteCtrl', function($scope, $route, $location) {
  $scope.$on('$routeChangeSuccess', function() {
    var path = $location.path();
    console.log(path);
    $scope.carsVisible = false;
    $scope.bikesVisible = false;
    if(path === '/cars') {
       $scope.carsVisible = true;
    } else if(path === '/bikes') {
       $scope.bikesVisible = true;
    }
  });
});

Plunker

此解决方案的想法来自@Andy

答案 1 :(得分:3)

我找到了另一种方式,我认为这是最简单,最快捷,最易管理的方式:

How to set bootstrap navbar active class with Angular JS?

这是:

使用ng-controller在ng-view之外运行单个控制器:

<div class="collapse navbar-collapse" ng-controller="HeaderController">
    <ul class="nav navbar-nav">
        <li ng-class="{ active: isActive('/')}"><a href="/">Home</a></li>
        <li ng-class="{ active: isActive('/dogs')}"><a href="/dogs">Dogs</a></li>
        <li ng-class="{ active: isActive('/cats')}"><a href="/cats">Cats</a></li>
    </ul>
</div>
<div ng-view></div>

并包含在controllers.js中:

function HeaderController($scope, $location) 
{ 
    $scope.isActive = function (viewLocation) { 
        return viewLocation === $location.path();
    };
}

答案 2 :(得分:1)

您应该使用ng-include;

,而不是使用ng-view

这将显示app/cars/index.htmlapp/bikes/index.html

的内容
<a href="#/cars">Cars</a>
<a href="#/bikes">Bikes</a>
<div ng-view></div>

请参阅http://docs.angularjs.org/tutorial/step_07

中的模板部分

答案 3 :(得分:1)

使用带有show指令的服务:

<div ng-show="myService.isActive('/')">Show this when at default route</div>
<div ng-show="myService.isActive('/cars')">Show this when at cars</div>

服务:

myApp.factory('MyService',
    function ($location) {
        return {
            isActive: function (path) {
                return (($location.path() == path));
            }
        }
    }
);

App.js:

// this is run after angular is instantiated and bootstrapped
myApp.run(function ($rootScope, myService) {
    $rootScope.breadcrumbService = MyService;
});