What is the meaning of # inside an Angular2 HTML directive - html

I'm trying to trigger the state of the sidenav (opened and closed). I have this:
<div class="viewport">
<md-toolbar color="primary">
<button *ngIf="!isNavbarOpen" md-icon-button (click)="sidenav.open()"><md-icon>menu</md-icon></button>
<button *ngIf="isNavbarOpen" md-icon-button (click)="sidenav.close()"><md-icon>close</md-icon></button>
<span class="toolbar-title">Title</span>
</md-toolbar>
<md-sidenav-layout>
<md-sidenav align="start" mode="over" #sidenav>
Sidenav
</md-sidenav>
</md-sidenav-layout>
</div>
How can I use the #sidenav from my TypeScript file to change the isNavbarOpen field whenever the sidenav state changes?
or
How could I override the sidenav.open() method?

Here is a better approach so you can use all the properties and subscribe to all the events. Don't pass it to the open()/close() function.
<div class="viewport">
<md-toolbar color="primary">
<button *ngIf="!isNavbarOpen" md-icon-button (click)="open()"><md-icon>menu</md-icon></button>
<button *ngIf="isNavbarOpen" md-icon-button (click)="close()"><md-icon>close</md-icon></button>
<span class="toolbar-title">Title</span>
</md-toolbar>
<md-sidenav-layout>
<md-sidenav align="start" mode="over" #sidenav>
Sidenav
</md-sidenav>
</md-sidenav-layout>
</div>
Instead references it by using "ViewChild" and MdSidenav:
import { Component, ViewChild, OnInit } from '#angular/core';
import { MdSidenav } from '#angular/material';
#Component({
....
})
export class AppComponent implements OnInit {
public isNavbarOpen: boolean;
// This gets a reference to the #sidenav
#ViewChild('sidenav') sidenav: MdSidenav;
ngOnInit() {
// Subscribe to the onCloseStart event
this.sidenav.onCloseStart.subscribe(() => {
this.isNavbarOpen = true;
});
}
open() {
this.sideNav.open();
}
}

Related

Angular | Bootstrap - Modal not showing

<img id="1" data-toggle="modal" data-target="#myModal" data-dismiss="modal" src='assets/barrel.jpg' alt='Text dollar code part one.' />
<div id="myModal" class="modal fade" *ngIf="isModalShowing">
<div class=" modal-lg center">
<div class="modal-header">
<button type="button" class="close" (click)="toggleModal()">×</button>
<div class="modal-content">
<div class="modal-body">
<img id="1" src="assets/barrel.jpg" class="img-responsive">
<img id="2" src="assets/car.jpg" class="img-responsive">
</div>
</div>
</div>
</div>
</div>
Above is my html, nothing happens when I click on the image that I am using as my modal trigger. This started when I add the ngIf="isModalShowing" function. Below is my typescript.
import { Component, OnInit, ViewChild } from '#angular/core';
import { NgModule } from '#angular/core';
#Component({
selector: 'app-portfolio',
templateUrl: './portfolio.component.html',
styleUrls: ['./portfolio.component.scss']
})
export class PortfolioComponent implements OnInit {
constructor() { }
public ngOnInit() {
}
isModalShowing: boolean;
toggleModal() {
this.isModalShowing = !this.isModalShowing;
}
}
This might help you as you are using pure bootstrap.
pay attention to [ngClass] in app.component.html file
I added modal-backdrop class to the class list that gives a visual indication of an active modal.
https://stackblitz.com/edit/angular-jggbzx
Or else use ng-bootstrap that has better ways of doing this
Documentation
https://ng-bootstrap.github.io/#/components/modal/examples
Running Example
https://stackblitz.com/edit/angular-dwqv1u

Angular Modal undefined nativeElement

I'm getting the following error when trying to close my modal:
cannot read property nativeElement of undefined
The element has been rendered by time i'm trying to close it so I'm not sure what else it could be.
Modal code:
<div class="modal" id="myModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<button type="button" class="btn btn-primary" (click)="close()">Close</button>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</div>
</div>
Typescript:
import { Component, OnInit, ElementRef, ViewChild } from '#angular/core';
#Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
constructor() { }
#ViewChild('myModal') closeModal: ElementRef;
ngOnInit() {
}
close() {
this.closeModal.nativeElement.click();
}
}
ViewChild uses a template reference variable, not the id, to refer to the element. You should set the variable myModal on the target element in the template markup:
<div class="modal" #myModal ...>
so that you can refer to that element with ViewChild:
#ViewChild('myModal') closeModal: ElementRef;

How to remove downscrollbar in pdf

import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-documents',
templateUrl: './documents.component.html',
styleUrls: ['./documents.component.less']
})
export class DocumentsComponent implements OnInit {
pdfSrc: string = '';
pdfZoom:number = 1;
constructor() { }
ngOnInit() {
this.pdfSrc = '/assets/demo.pdf';
}
public zoomIn()
{
this.pdfZoom += 0.25;
}
public zoomOut()
{
if (this.pdfZoom > 1) {
this.pdfZoom -= 0.25;
}
}
}
<div class="m-portlet m-portlet--mobile">
<div class=modal-body align="center">
<span class="input-group-btn">
<button class="btn" name="zoomout" (click)="zoomOut()"><i class="fa fa-search-minus"> </i></button>
<button class="btn" name="zoomin" (click)="zoomIn()"><i class="fa fa-search-plus"> </i></button>
</span>
</div><br>
<div class="m-portlet__body" style="overflow-x:scroll; overflow-y: scroll;max-height:75vh;">
<pdf-viewer [src]="pdfSrc" [render-text]="true" [original-size]="false" [zoom]="pdfZoom" [page]="page"
[show-all]="true" [autoresize]="true" style="display: block;">
</pdf-viewer>
</div>
</div>
<!--<img src="assets/common/images/sampleProfilePics/a.jpg" height="432" width="405" />-->
I created a layout. On that I'm using ng2-pdfviewer package to add pdf documents on layout page,and with that created zoomin and zoom out button,and overflow-x and overflow-y scrollbars.but overflow-y scrollbar only working but overflow-x is not working why because horizontal scroll bar is coming with pdf itself.
So,I want to remove that predifined horizontal scroll bar.

How to set image url in html from typescript?

I'm building ionic app for wallpaper/gallery.I want app to show wallpaper in full screen when user clicks on a particular image.
Thanks.
HTML code:
<ion-header>
<ion-navbar color="dark">
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Walldo</ion-title>
</ion-navbar>
</ion-header>
<ion-slides zoom="true">
<ion-slide>
<div class="swiper-zoom-container">
<img id="fullwall" src="some link">
</div>
<ion-label>Elephant</ion-label>
<button ion-button round (click)="download('wall.jpg')">Download</button>
</ion-slide>
</ion-slides>
TYPESCRIPT code:
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
#IonicPage()
#Component({
selector: 'page-popular',
templateUrl: 'popular.html',
})
export class PopularPage {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad PopularPage');
}
}
It's a simple property binding:
<img [src]="theWallImageUrl" />
and in the code of the component:
this.theWallImageUrl = ...

How to ovveride the css class of root component in its child component - Angular 2?

I have 3 components in angular 2 application.The class="container-fluid content" is css class in app.component.html. This class="container-fluid content" is default css for other components. Now I want to set background-color:blue in the detail component. I tried to set in detail.component.ts like this styles:['.container-fluid content{background-color: blue;}'] It did not work. If I set in app.component.html like this <div class="container-fluid content" style="background-color: blue;"> It applies to both the components. How to override this class="container-fluid content" in detail component?
//my project structure
app
- app.component.html
-app.component.ts
- welcome
-welcome.component.html
-welcome.component.ts
- detail
-detail.component.html
-detail.component.ts
//app.component.html
<div class="container-fluid content">
<router-outlet></router-outlet>
</div>
<app-footer></app-footer>
</div>
//welcome.component.html
<h1>welcome page heading</h1>
<div fxLayout="row" fxLayoutWrap style="padding-bottom: 25px;
padding-top: 25px; margin: auto;justify-content: center" >
<md-card>
<md-card-content>
<h1></h1>
<h2></h2>
<h2>
</h2>
</md-card-content>
</md-card>
</div>
//detail.component.html
<h1>Details page heading</h1>
<div fxLayout="row" fxLayoutWrap style="padding-bottom: 25px;
padding-top: 25px; margin: auto;justify-content: center" >
<md-card>
<md-card-content>
<h1></h1>
<h2></h2>
<h2>
</h2>
</md-card-content>
</md-card>
</div>
//detail.component.ts
import { OnInit, Component } from '#angular/core';
import { DetailService } from '../detail/detail.service';
import { HostBinding} from '#angular/core';
#Component({
providers: [DetailService ]
templateUrl: './detail.component.html',
styles: ['h3 {margin:5px}']
})
export class DetailComponent implements OnInit {
#HostBinding('class.blueClass') blue: boolean = false;
constructor( private _detailService: DetailService ) { }
ngOnInit() {
this.blue = true;
}
}
In child component, you can add this param to #Component.
// child-component.component.ts
#Component({
selector: 'child-component',
templateUrl: 'child-component.component.html',
styleUrls: ['child-component.component.css']
encapsulation: ViewEncapsulation.None
})
// child-component.component.css
.container-fluid content{background-color: blue;}
You can ref this side for more infomartion :
https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html
Have you tried with the hostbinding and adding a new class there?
#HostBinding('class.blueClass') blue: boolean = false;
And in the second component just switch that on onInit?
ngOnInit() {
this.blue = true;
}
The other way could be in the component definition, you can add the following line :
host: {'class': 'blueClass'}
and you do the rest of the css work in css instead.