如何正确地实施与角UI路由器多个视图?多个、视图、路由器、正确地

2023-09-13 03:47:57 作者:时光取名叫无心

我想实现两个主要内容的窗格网站(想象一下两列的页面的每个宽度的一半)。该窗格应该能够改变相互独立的,有自己的控制器,并能相互之间传递数据(因此而右边的窗格中保持不变,用户可以通过一帮在左窗格中的页面导航无需重新加载和反之亦然,并在左窗格中的变化可以更新数据显示在右边窗格中向上)。

我已经通过了一堆的UI路由器的例子/教程看了,但似乎无法找到如何实现我想要做的一个很好的例子。所有这些似乎表明要么只是单一嵌套视图的例子(查看大图内,以一个单一的视图)或显示多个视图,而不是去改变相互独立的意见。

我想用一个做到这一点<格UI的视图>< / DIV> index.html文件中(而不是多个UI -views),这样我可以有单亲UI的视图中发生的一切,而不是在index.html文件。

现在我有类似如下的js文件:

 配置(['$ stateProvider','$ urlRouterProvider',函数($ stateProvider,$ urlRouterProvider){      $ urlRouterProvider.otherwise('/家);      $ stateProvider      .STATE('家',{            网址:'/家,            观点:{                '':{templateUrl:意见/ home.html做为},                '离开家': {                  templateUrl:查看/图表/ chartHome.html',                },                正确@家:{                    templateUrl:意见/ view1.html',                    控制器:'View1Ctrl                }            }        });    }]); 

和下面的HTML为home.html的:

 < D​​IV>    < H1>这是家< / H1>< / DIV><格UI视图='左'>< / DIV><格UI视图='右'&GT​​;< / DIV> 

任何人都可以在这方面帮助?或者,这是错误的方式来思考这个网站?如果是的话我该怎么做?

谢谢!

解决方案 双路由器覆盖方案 双路由器有线级联覆盖方案

这是pretty并发UI的路由器来解决。

我已经创造了这个小提琴给UI路由器嵌套状态的尝试。它的工作原理,但它正在迅速流汗难看。

它的工作原理是这样的: home.main.left 路由变化只有左视图,并会使用 home.main 。这将是最好能有相互独立的两个视图状态,但我不知道怎么做,与UI的路由器。

我认为这将是更好地使用UI的路由器上做了左侧的导航和使用权的一部分(或者反过来)的自定义指令。沟通,您可以使用指令 $排放/ $广播

更改左边的路线也可以从链接指令的UI SREF $ state.go(... )方法。

请看看下面或在演示这个的jsfiddle 。

\r\r

angular.module('demoApp',['ui.router'])\r的.config(['$ stateProvider','$ urlRouterProvider',函数($ stateProvider,$ urlRouterProvider){\r      $ urlRouterProvider.otherwise('/家);\r      $ stateProvider\r      .STATE('家',{\r          templateUrl:意见/ home.html做为',\r          摘要:真\r      })\r      .STATE('home.main',{\r            网址:'/家,\r            templateUrl:查看/图表/ chartHome.html',\r          控制器:'LeftCtrl\r        })\r      .STATE('home.left',{\r            模板:home.left< A HREF =#UI-SREF =home.left1> LEFT1变化< / A>< BR /> {{你好}}',\r          控制器:'LeftCtrl\r          \r        })\r      .STATE('home.left1',{\r            模板:home.left1< A HREF =#UI-SREF =home.left2> LEFT2变化< / A>'\r        })\r      .STATE('home.left2',{\r            模板:home.left2< A HREF =#NG点击=triggerDirective()>改变指令< / A>,\r          控制器:函数($范围){\r          $ scope.triggerDirective =功能(){\r            的console.log('做指令行动);\r                。$ $范围发出('rightContent:changeMsg','新内容');\r                //发出来rootscope因为rightcontent不是leftcontent的孩子\r            };\r          }\r        })\r      \r      \r    }])\r.directive('rightContent',函数(){\r返回{\r    限制:EA,\r        模板:从指令一些静态内容或其他的东西。 <强> {{}信息}< / STRONG>,\r        控制器:函数($范围,$ rootScope){\r            $ scope.message ='你好,从指令。';\r        $ rootScope在$('rightContent:changeMsg',函数(EVT,消息){\r                的console.log(消息);\r            $ scope.message =消息;\r            });\r        }\r    }\r})\r.controller('LeftCtrl',函数($范围){\r    \r    $ scope.hello ='你好左';\r})\r.controller('AppController的',函数($范围){\r});

\r

。左{\r    浮动:左;\r    宽度:50%;\r    高度:200像素;\r    背景:浅绿;\r}\r\r\r。对 {\r    浮动:权利;\r    宽度:50%;\r    高度:200像素;\r    背景:浅灰色;\r}

\r

&LT;脚本SRC =htt​​ps://cdnjs.cloudflare.com/ajax /libs/angular.js/1.4.4/angular.js\"></script>\r&LT;脚本src=\"https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js\"></script>\r&LT;链接HREF =htt​​ps://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css的rel =stylesheet属性/&GT;\r\r&LT; D​​IV NG-应用=demoAppNG控制器=AppController的&GT;\r    &LT;脚本类型=文/ NG-模板ID =意见/ /home.html的&GT;\r        &LT; D​​IV&GT;\r            &LT; H1&gt;这是家&LT; / H1&GT;\r        &LT; / DIV&GT;\r        &LT; D​​IV CLASS =包装&GT;\r            &LT;格UI视图=级=左&GT;\r            &LT; / DIV&GT;\r            &LT; D​​IV CLASS =右&GT;\r                &LT;对内容和GT;&LT; /右内容&GT;\r            &LT; / DIV&GT;\r            &LT; A HREF =#UI-SREF =home.main&GT;家庭和LT; / A&GT;\r        &LT; / DIV&GT;\r    &LT; / SCRIPT&GT;\r    &LT;脚本类型=文/ NG-模板ID =查看/图表/ chartHome.html&GT;\r        左{{你好}}\r        &LT; A HREF =#UI-SREF =home.left&GT;左变化与LT; / A&GT;\r    &LT; / SCRIPT&GT;\r    &LT;脚本类型=文/ NG-模板ID =意见/ view1.html&GT;\r        右{{你好}}\r    &LT; / SCRIPT&GT;\r    \r    &LT;格UI视图=&GT;&LT; / DIV&GT;\r&LT; / DIV&GT;

\r\r\r

I'm trying to implement a site with two main content 'panes' (imagine two columns each half the width of the page). The panes should be able to change independent of each other and have their own controllers and be able to pass data between each other (so the user can navigate through a bunch of pages on the left pane while the right pane stays the same without reloading and vice versa, and a change in the left pane can update data showing up in the right pane).

I've looked through a bunch of examples/tutorials on UI Router but can't seem to find a good example of how to implement what I'm trying to do. All of them seem to show either just examples for single nested views (so a single view within a larger view) or show multiple views but not how to change the views independent of each other.

I'd like to do this with a single <div ui-view></div> in the index.html file (as opposed to multiple ui-views) so that I can have everything happening within the single parent ui-view and not in the index.html file.

Right now I have something like the following js file:

config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise('/home');
      $stateProvider
      .state('home', {
            url: '/home',
            views: {
                '': { templateUrl: 'views/home.html' },
                'left@home': { 
                  templateUrl: 'views/charting/chartHome.html',
                },
                'right@home': { 
                    templateUrl: 'views/view1.html',
                    controller: 'View1Ctrl'
                }
            }

        });
    }]);

and the following HTML for home.html:

<div>
    <h1>This is home</h1>
</div>
<div ui-view='left'>
</div>
<div ui-view='right'>
</div>

Can anyone help with this? Or is this the wrong way to be thinking about the site? If so what should I be doing?

Thanks!

解决方案

This is pretty complicated to solve with ui-router.

I've created this fiddle to give ui-router nested states a try. It works but it is gettting ugly quickly.

It works like this: home.main.left route changes only the left view and uses the right view from home.main. It would be better to have both view states independent from each other but I'm not sure how to do that with ui-router.

I think it would be better to use ui-router to do the navigation of the left part and use a custom directive for the right part (or the other way round). To communicate to the directive you can use $emit/$broadcast.

Changing the route of the left part is also possible from the directive with links ui-sref or with $state.go(...) method.

Please have a look at the demo below or in this jsfiddle.

angular.module('demoApp', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise('/home');
      $stateProvider
      .state('home', {
          templateUrl: 'views/home.html',
          abstract: true
      })
      .state('home.main', {
            url: '/home',
            templateUrl: 'views/charting/chartHome.html',
          	controller: 'LeftCtrl'
        })
      .state('home.left', {
            template: 'home.left <a href="#" ui-sref="home.left1">left1 change</a><br/>{{hello}}',
          controller: 'LeftCtrl'
          
        })
      .state('home.left1', {
            template: 'home.left1 <a href="#" ui-sref="home.left2">left2 change</a>'
        })
      .state('home.left2', {
            template: 'home.left2 <a href="#" ng-click="triggerDirective()">change directive</a>',
          controller: function($scope) {
          	$scope.triggerDirective = function() {
            	console.log('do directive action');
                $scope.$emit('rightContent:changeMsg', 'new content');
                // emit to rootscope because rightcontent is not a child of leftcontent
            };
          }
        })
      
      
    }])
.directive('rightContent', function() {
	return {
    	restrict: 'EA',
        template: 'Some static content or other stuff from directive. <strong>{{message}}</strong>',
        controller: function($scope, $rootScope) {
            $scope.message = 'hello from directive.';
        	$rootScope.$on('rightContent:changeMsg', function(evt, message) {
                console.log(message);
            	$scope.message = message;
            });
        }
    }
})
.controller('LeftCtrl', function($scope){
    
    $scope.hello = 'hello from left';
})
.controller('appController', function($scope) {
});

.left {
    float: left;
    width: 50%;
    height: 200px;
    background: lightgreen;
}


.right {
    float: right;
    width: 50%;
    height: 200px;
    background: lightgray;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<div ng-app="demoApp" ng-controller="appController">
    <script type="text/ng-template" id="views/home.html">
        <div>
            <h1>This is home</h1>
        </div>
        <div class="wrapper">
            <div ui-view="" class="left">
            </div>
            <div class="right">
                <right-content></right-content>
            </div>
            <a href="#" ui-sref="home.main">home</a>
        </div>
    </script>
    <script type="text/ng-template" id="views/charting/chartHome.html">
        left {{hello}}
        <a href="#" ui-sref="home.left">left change</a>
    </script>
    <script type="text/ng-template" id="views/view1.html">
        right {{hello}}
    </script>
    
    <div ui-view=""></div>
</div>