[英]How can I use Angular 2 Material Sidenav inside a component?
I'm new to angular and I'm trying to understand the ropes here. 我是新手,我正在尝试了解这里的绳索。 The docs of the new angular 2 material are a bit confusing but I managed to add tabs and the sidenav to my app but I can't make a button to open the sidenav using my component.
新的angular 2材质的文档有些混乱,但是我设法向应用程序中添加了选项卡和sidenav,但是无法使用我的组件创建按钮来打开sidenav。 This example is the only reference I have for how it's done and not knowing much about it I think it's using angular 1?
这个例子是我唯一的参考,关于它是如何完成的,并且我对此不太了解,我认为它使用的是角度1? https://material.angularjs.org/latest/demo/sidenav
https://material.angularjs.org/latest/demo/sidenav
What I have right now in my component is this: 我现在在组件中拥有的是:
import { Component, OnInit, OnDestroy, ElementRef, Inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {Http, Response} from '@angular/http';
import { MD_TABS_DIRECTIVES } from '@angular2-material/tabs';
import { MD_SIDENAV_DIRECTIVES } from '@angular2-material/sidenav';
@Component({
selector: 'my-ambient-detail',
templateUrl: 'app/ambients/ambient-detail.component.html',
directives: [MD_TABS_DIRECTIVES, MD_SIDENAV_DIRECTIVES],
})
export class AmbientDetailComponent implements OnInit, OnDestroy {
// constructor and other functions here
openRightMenu(){
//WHAT GOES HERE?
}
}
And in the html: 并在html中:
<md-sidenav-layout>
<!-- Content -->
<md-content>
<md-button (click)="openRightMenu()">
Open Right Menu
</md-button>
</md-content>
<!-- Sidebar -->
<md-sidenav md-component-id="right" class="md-sidenav-right">
Sidebar content
</md-sidenav>
</md-sidenav-layout>
So I can't find a clear reference on how to open/close the sidenav with a button using functions in the component. 所以我找不到关于如何使用组件中的功能通过按钮打开/关闭sidenav的明确参考。 Maybe I'm doing this all wrong, I built my app starting straight from the heroes tutorial in the angular website but I'm very confused between angular 1 and 2 from my searches on this topic.
也许我做错了所有事,我直接从angular网站上的英雄教程开始构建了我的应用程序,但是我对这个主题的搜索对角度1和角度2非常困惑。
You need to grab a ViewChild reference to element by id (#sidenav), and type it to the MdSidenav component. 您需要获取一个按ID(#sidenav)对元素的ViewChild引用,并将其键入到MdSidenav组件。 Note that the ViewChild reference is not available yet at constructor, but only after init.
请注意,ViewChild引用在构造函数上尚不可用,仅在初始化之后可用。
import { Component, ViewChild, OnInit } from '@angular/core';
import { MdSidenav } from '@angular/material';
@Component({
...,
template: `
<md-sidenav-container>
<md-sidenav #sidenav mode="side" opened="false"></md-sidenav>
</md-sidenav-container>
`
})
export class MyComponent implements OnInit {
@ViewChild('sidenav') sidenav: MdSidenav;
constructor() {}
ngOnInit() {
this.sidenav.onOpen.subscribe(() => {
console.log("Sidenav opened");
});
setTimeout(this.openSidenav.bind(this), 5000);
}
openSidenav() {
this.sidenav.open();
}
}
Apparently that was indeed angular 1, I finally found this: https://justindujardin.github.io/ng2-material/#/components/sidenav that seems to be using angular 2 and I didn't even need to have a function in my component: 显然那确实是有角的1,我终于找到了: https : //justindujardin.github.io/ng2-material/#/components/sidenav似乎正在使用有角2,我什至不需要在我的组件:
<md-sidenav-layout>
<!-- Content -->
<md-content>
<md-button (click)="right.open()">
Open Right Menu
</md-button>
</md-content>
<!-- Sidebar -->
<md-sidenav #right md-component-id="right" class="md-sidenav-right">
Sidebar content
</md-sidenav>
</md-sidenav-layout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.