【问题标题】:Show/Hide using custom breakpoints and angular/flex-layout使用自定义断点和 angular/flex-layout 显示/隐藏
【发布时间】:2018-05-04 03:09:27
【问题描述】:

我正在使用 Angular(v5.0.2) 和 Angular-FlexLayout (v2.0.0-beta.10-4905443)。我正在尝试对@angular/flex-layout 提供的每个指令使用自定义断点。

我添加了自定义断点:​​

import {DEFAULT_BREAKPOINTS, BREAKPOINTS} from '@angular/flex-layout';

const CUSTOM_BREAKPOINTS = [
  {
    alias: 'sm.landscape',
    suffix: 'SmLandscape',
    mediaQuery: 'screen and (min-width:55em) and (max-width: 80em) and (orientation: landscape)',
    overlapping: true
  }
];

export const CustomBreakPointsProvider = {
  provide: BREAKPOINTS,
  useValue: [...DEFAULT_BREAKPOINTS,...CUSTOM_BREAKPOINTS],
};

我已经扩展了 ShowHideDirective:

import { Directive, ElementRef, Renderer2, Input, Optional, Self } from '@angular/core'
import { ShowHideDirective, negativeOf, LayoutDirective, MediaMonitor } from '@angular/flex-layout'

@Directive({
  selector: `
    [fxHide.sm.landscape],
    [fxShow.sm.landscape],
  `
})
export class CustomShowHideDirective extends ShowHideDirective {
  constructor(monitor: MediaMonitor, @Optional() @Self() protected _layout: LayoutDirective, protected elRef: ElementRef, protected renderer: Renderer2) {
    super(monitor, _layout, elRef, renderer)
  }

  @Input('fxHide.sm.landscape') set hideSmLandscape(val) {this._cacheInput("showSmLandscape", negativeOf(val))}
  @Input('fxShow.sm.landscape') set showSmLandscape(val: boolean) {this._cacheInput("showSmLandscape", <boolean>val)}
}

删除@Optional() @Self() 以靠近doc 会触发错误:No provider found for LayoutDirective。将LayoutDirective, ElementRef 添加到提供程序会触发与here 相同的错误。因此,我在source code 之后扩展了类。

我使用我在 app.module 中定义的内容:

@NgModule({
  declarations: [
    AppComponent,
    CustomShowHideDirective,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpModule,
    FlexLayoutModule,
  ],
  providers: [
    CustomBreakPointsProvider
  ],
  bootstrap: [AppComponent]
})

我根据屏幕大小显示或隐藏元素:

<p>
  <span fxHide="false" fxHide.sm="true">fxHide.sm</span>
  <span fxShow="false" fxShow.sm="true">fxShow.sm</span>
</p>
<p>
  <span fxHide="false" fxHide.sm.landscape="true">fxHide.sm.landscape</span>
  <span fxShow="false" fxShow.sm.landscape="true">fxShow.sm.landscape</span>
</p>
<p>
  <span fxHide="true" fxHide.sm.landscape="false">hack fxShow.sm.landscape</span>
  <span fxShow="true" fxShow.sm.landscape="false">hack fxHide.sm.landscape</span>
</p>
<p>
  <span fxHide fxShow.sm.landscape>hack2 fxShow.sm.landscape</span>
  <span fxShow fxHide.sm.landscape>hack2 fxHide.sm.landscape</span>
</p>
<p>
  <span fxHide="false" fxHide.gt-sm="true">fxHide.gt-sm</span>
  <span fxShow="false" fxShow.gt-sm="true">fxShow.gt-sm</span>
</p>
<span *ngIf="media.isActive('sm')">sm</span>
<span *ngIf="media.isActive('sm.landscape')">sm.landscape</span>
<span *ngIf="media.isActive('md')">md</span>
<span *ngIf="media.isActive('lg')">lg</span>

因此,预期的行为如下: - 无论屏幕大小如何,每行只显示一个项目,除了最后一个项目,其中显示所有可用的 mediaQueries - 根据屏幕大小在每一行显示 fxHide 或 fxShow

实际行为如所附屏幕截图所示:Large screenCustom media query activatedSmall screen

您看到 ShowHideDirective 不再适用于默认断点。此外,我可以在自定义断点处于活动状态时隐藏元素,但相反的行为不起作用。

我已经将我所有的 npm 包更新到最新可用的,删除 node_modules 文件夹并再次运行npm install,但没有效果。

我还尝试将LayoutDirective, ElementRef 添加到提供者列表中。然后我得到与here 相同的错误。

在我的CustomShowHideDirectiveshowSmLandscape 方法中添加打印表明该方法确实被调用,并且具有正确的参数(据我所知)。 fxShow 也是如此。

showhide(直接在我的node_modules 文件夹中)添加相同类型的调试表明ShowHideDirective 是用正确的值调用的。

欢迎任何想法/提示/评论。由于从 rxjs 加载运算符时出错...

编辑:终于可以使用工作的 plunker here (工作意味着显示意外行为^^)

感谢您的任何贡献;)

【问题讨论】:

    标签: angular angular-flex-layout


    【解决方案1】:

    这似乎是过时文档和错误的组合:请参阅 this issue on the github repo,引用 more precise bug

    希望这将很快得到解决;)

    【讨论】:

      猜你喜欢
      • 2020-07-05
      • 1970-01-01
      • 2018-06-02
      • 2020-08-22
      • 2016-05-14
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多