본문 바로가기

엉터리 개발 이야기/angular2+

[Angular2+] 새창에서 팝업창 열기

반응형
<a> 태그로 target="_blank" 로 하여 새로운 창을 여니 새탭으로 열린다.
팝업으로 띄우려면 window.open(url, title, option)을 하면 되는데
Angular에서는 어떻게 하나 찾아보니 아래와 같았다.

app.component.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Component, OnInit } from '@angular/core';
import {WindowRef} from './windowRef';
 
@Component({
  selector: 'app-root',
  
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  
})
 
export class AppComponent implements OnInit {
 
  title = 'app';
 
  constructor( private winRef: WindowRef ) { 
      console.log(winRef.nativeWindow);
  }
 
  openWindow() {
    const popOption = 'width=300';
    this.winRef.nativeWindow.open('http://google.co.kr'""popOption);
  }
  
  ngOnInit(): void {
  
  };
    
}
cs


app.component.html

1
<a (click)="openWindow()">Click</a>
cs


windowRef.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import {Injectable} from '@angular/core';
 
function _window(): any {
  // return the native window obj
  return window;
}
 
@Injectable()
export class WindowRef {
  
  get nativeWindow(): any {
    return _window();
  }
  
}
cs


새창으로 팝업이 열린다~

반응형