【问题标题】:Dynamically switch ng-include between controllers在控制器之间动态切换 ng-include
【发布时间】:2014-06-12 22:40:50
【问题描述】:

我的导航有以下代码,我想在页面之间动态更新。

<nav ng-include="menuPath"></nav>

这是我的应用和路由设置

var rxApp = angular.module('ehrxApp', ['ngRoute']);

// configure our routes
rxApp.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'mainController',
            templateUrl: '/content/views/index.html'
        })
        .when('/census', {
            templateUrl: '/content/views/admission/census.html',
            controller: 'censusController'
        })
        .when('/messages', {
            templateUrl: '/content/views/account/messages.html',
            controller: 'messagesController'
        })
        .when('/profile', {
            templateUrl: '/content/views/account/profile.html',
            controller: 'profileController'
        })
});

在我的主控制器中,我在这里设置了 menuPath 值:

rxApp.controller('mainController', function (userService, $scope, $http) {
    evaluate_size();

    $scope.menuPath = "/content/views/index.menu.html";
});

rxApp.controller('censusController', function ($scope, $http, $sce, censusService) {
    $scope.menuPath = "/content/views/admission/census.menu.html";
    evaluate_size();

});

当页面切换到人口普查视图时,它应该更改菜单。但是会发生什么是第一页加载主菜单,然后无论您转到菜单的其他页面都不会更新。

【问题讨论】:

  • 需要更多地了解您的 html 以提供帮助

标签: angularjs angularjs-scope angularjs-routing angularjs-ng-include


【解决方案1】:

我想这个问题与原始值和子范围之间的原型继承有关,但需要查看更多的 html 来确定这一点。如果没有这个,我提出了一种替代方法,可以解决您的问题并将配置全部保存在一个地方。

$routeProvider 将接受变量并将它们保留在路由上,即使 Angular 不使用它们。所以我们通过包含 menuPath 来修改您的路由,如下所示:

var rxApp = angular.module('ehrxApp', ['ngRoute']);

// configure our routes
rxApp.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'mainController',
            templateUrl: '/content/views/index.html',
            menuPath: '/content/views/index.menu.html'
        })
        .when('/census', {
            templateUrl: '/content/views/admission/census.html',
            controller: 'censusController',
            menuPath: '/content/views/admission/census.menu.html'
        })
        .when('/messages', {
            templateUrl: '/content/views/account/messages.html',
            controller: 'messagesController'
        })
        .when('/profile', {
            templateUrl: '/content/views/account/profile.html',
            controller: 'profileController'
        })
});

从每个控制器中删除 $scope.menuPath 设置,然后最后在 rootScope 上添加一个监视,它将更改 $routeChangeSuccess 上的 menuPath

rxApp.run(['$rootScope', function ($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function(event, current) {
        if (current && current.$$route && current.$$route.menuPath) {
            $rootScope.menuPath = current.$$route.menuPath;
        } else {
            $rootScope.menuPath = '';
        }
    });
}]);

【讨论】:

  • 效果很好。我还是 Angular 的新手,所以其中一些技巧我还没有学会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 2015-09-07
  • 2012-12-27
  • 2012-05-09
  • 1970-01-01
相关资源
最近更新 更多