简体   繁体   English

如何根据用户当前所在的页面在Web应用程序的右侧栏中动态添加数据?

[英]How to add data dynamically on the right sidebar of my web application depending on the page the user is currently in?

I have developed a web app in angular.js. 我已经在angular.js中开发了一个Web应用程序。 My question is that I have the menu on left sidebar that remains constant. 我的问题是我在左侧边栏上的菜单保持不变。 However, the data on the right sidebar changes depending on the page that the user is currently in. If the user is on home page, then the right sidebar will show, for example, "data 1". 但是,右侧栏上的数据会根据用户当前所在的页面而变化。如果用户位于主页上,则右侧栏上将显示例如“数据1”。 If the user is on analytics page, then the right sidebar will show a graph. 如果用户在分析页面上,则右侧栏将显示一个图形。 I do not know how to implement this using angular in the most optimized way. 我不知道如何以最佳方式使用angular来实现这一点。

Any kind of help is greatly appreciated. 任何帮助都将不胜感激。

Thanks in advance 提前致谢

You have to implement the angular routing: 您必须实现角度路由:

  1. Keep the html element as a container (inside to right side bar) provided with a ng-view directive 将html元素保留为带有ng-view指令的容器(在右侧栏内)

  2. You have to create different html template in separate folder, which will be rendered as per the menu selection 您必须在单独的文件夹中创建不同的html模板,该模板将根据菜单选择进行渲染

  3. Write .config() , where you write down your specific logic to associate each state/ templateurl to particular controller and template 编写.config() ,在其中写下您的特定逻辑以将每个状态/ templateurl与特定控制器和模板相关联

You should have a look at the ngInclude directive. 您应该看看ngInclude指令。 That might be the best way for you to load a template (and the data in your corresponding controller). 这可能是您加载模板(以及相应控制器中的数据)的最佳方法。 L 大号

you can use ngRoute and call your separate html template and controller for each page like this 您可以使用ngRoute并为每个页面调用单独的html templatecontroller ,如下所示

angular
.module('myApp', ['ngRoute'])
.config(['$routeProvider', function($routeProvider){
$routeProvider
  .when('/', {
    templateUrl: 'home.html',
    controller: ['$scope', function($scope){
      $scope.page = 'Data 1';
    }]
  })
  .when('/analytics', {
    templateUrl: 'analytics.html',
    controller: ['$scope', function($scope){
      $scope.page = 'Show Graph';
    }]
  })
  .otherwise({redirectTo: '/'});
}]);

then add ng-view in your html like this 然后像这样在您的html中添加ng-view

<div class="content">
    <div ng-view></div>
</div>

working demo plunker 演示样机

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

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