我正在使用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 screen,Custom media query activated,Small screen。
您会看到ShowHideDirective不再适用于默认断点。另外,我可以在自定义断点处于活动状态时隐藏元素,但相反的行为不起作用。
我已将所有npm软件包更新为最新版本,删除node_modules文件夹并再次运行npm install
,但不起作用。
我还尝试将LayoutDirective, ElementRef
添加到提供商列表中。然后我得到与here相同的错误。
在showSmLandscape
的{{1}}方法中添加打印件表明该方法确实被调用,并且使用正确的参数(据我所知)。 fxShow也是一样。
在CustomShowHideDirective
和show
(直接在我的node_modules文件夹中)中添加相同类型的调试,表明使用正确的值调用hide
。
欢迎任何想法/提示/评论。由于从rxjs加载运算符的错误,我还没设法创建一个plunker ...
编辑:终于可以使用工作的plunker here(工作意味着显示意外行为^^)
感谢您的贡献;)