使用 UI-Router 时,Angular Material 布局会中断 [英] Angular Material layout breaks when using UI-Router

查看:22
本文介绍了使用 UI-Router 时,Angular Material 布局会中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Angular 1.5 项目中使用 Angular Material 1.1.0 和 UI-Router 1.0.0-beta.1,而 UI-Router 似乎破坏了 flexbox 功能.

当 index.html 布局不包含 UI-Router 元素时,它会拉伸并填充容器.当我添加 <div ui-view="container"></div> 时,布局会中断.

当我有以下情况时 Flex 正在工作:

<div layout="row" flex><div flex class="red">行中的第一项</div><div flex class="blue">行中的第二项</div>

检查时,它显示添加了 flex 类:

<div flex class="red flex">行中的第一项</div><div flex class="blue flex">行中的第二项</div>

但是当我添加 UI-Router 时,它会在页面顶部显示两行,并且元素不会垂直弯曲.index.html 中的代码:

<div class="gradient flex"><div ui-view="container" flex></div>

在容器中:

<div flex class="red">行中的第一项</div><div flex class="blue">行中的第二项</div>

检查后发现缺少 flex 类:

<!-- uiView: 容器 --><div ui-view="container" flex class="ng-scope flex"><stream class="ng-scope ng-isolate-scope"><div layout="row"><div flex class="red">行中的第一项</div><div flex class="blue">行中的第二项</div>

我知道布局只会影响该容器的直接子级的流向,而 UI-Router 正在添加 <stream class="ng-scope ng-isolate-scope">.我如何能够将 flex 类添加到 UI-Router 视图?

解决方案

为了避免陷入困境,我基本上使用以下约定解决了我的问题:

<!-- 顶部工具栏(屏幕顶部)--><md-toolbar></md-toolbar><!-- 这个 div 包含我的 sidenav 和主要内容 --><div flex layout="row"><!-- 这是我的sidenav--><md-sidenav>...</md-sidenav><!-- 这是我的主要内容,适合一个ng-view元素--><div flex ng-view></div>

那么这里发生了什么?我希望我的工具栏(在顶部)与我的 sid-nav 和主要内容列内.由于我将 视为我的父元素,因此我将其属性设置为 layout=column.现在我有顶部的工具栏和 <div> 将侧面导航和主要内容堆叠在列"中,我想制作这个 <div>一行,这样我就可以并排堆叠导航栏和主要内容.保存这些的父

得到 layout=row.这个

的子元素是 sidenav 和主要内容,因此它们获得属性 flex.请注意,我没有在 md-sidenav 上指定 flex;Angular Material 为我解决了这个问题.哇.那是相当多的.看看那个,看看它是否有意义.我也很乐意帮助您在 JSFiddle 或 Plnkr 上解决这个问题.请告诉我!

I´m using Angular Material 1.1.0 with UI-Router 1.0.0-beta.1 in my Angular 1.5 project and UI-Router seems to break flexbox functionality.

The index.html layout stretches and fills the container when it doesn't contain UI-Router element. When I add <div ui-view="container"></div> the layout breaks.

Flex is working when I have:

<body ng-app="app" layout="column" ng-cloak>
  <div layout="row" flex>
    <div flex class="red">First item in row</div>
    <div flex class="blue">Second item in row</div>
  </div>
</body>

When inspected it displays that flex class is added:

<div layout="row" flex= class="layout-row flex">
   <div flex class="red flex">First item in row</div>
   <div flex class="blue flex">Second item in row</div>
</div>

But when I add UI-Router, it displays two rows at the top of the page and elements aren't flexing vertically. The code in index.html:

<body ng-app="app" layout="column" ng-cloak>
  <div class="gradient flex">
    <div ui-view="container" flex></div>
  </div>
</body>

And in container:

<div layout="row" flex>
  <div flex class="red">First item in row</div>
  <div flex class="blue">Second item in row</div>
</div>

When inspected it reveals flex class is missing:

<div class="gradient flex">
  <!-- uiView: container -->
  <div ui-view="container" flex class="ng-scope flex">
    <stream class="ng-scope ng-isolate-scope">
      <div layout="row">
        <div flex class="red">First item in row</div>
        <div flex class="blue">Second item in row</div>
      </div>
    </stream>
  </div>
</div>

I'm aware that layout only affects the flow direction for that container's immediate children and UI-Router is adding <stream class="ng-scope ng-isolate-scope">. How I´m able to add the flex class to UI-Router views?

解决方案

Without getting too far down the rabbit hole, I essentially fixed my issue using this convention:

<body ng-app="app" layout="column">
  <!-- Top tool bar (top of screen) -->
  <md-toolbar></md-toolbar>

  <!-- This div holds both my sidenav and main content -->
  <div flex layout="row">

    <!-- This is my sidenav -->
    <md-sidenav>...</md-sidenav>

    <!-- This is my main content, fit into an ng-view element -->
    <div flex ng-view></div>
</div>

So what's going on here? I want my toolbar (at the top) to be in-column with my sid-nav and main content. Since I'm treating <body> as my parent element, I give it the attribute of layout=column. Now that I have the toolbar on top and the <div> that holds both the side-nav and main content stacked underneath in a 'column,' I want to make this <div>a row so I can stack nav-bar and main content side-by-side. The parent <div> that holds these gets layout=row. The children of this <div> are the sidenav and main content, so they get the attribute flex. Notice that I didn't specify flex on the md-sidenav; Angular Material takes care of that for me. Whew. That was quite a bit. Look at that and see if it makes sense. I would also be delighted to help you figure this out on a JSFiddle or Plnkr. Just let me know!

这篇关于使用 UI-Router 时,Angular Material 布局会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆