2017-06-09 37 views
0

我試圖調用getInput()通過onclick事件方法保存按鈕。但是這會產生以下錯誤:調用方法()

Uncaught TypeError: this.getInput is not a function at HTMLButtonElement.onclick ((index):1) 

我想調用getInput()方法。我也嘗試致電getInput()沒有這個。任何建議將不勝感激。

this.renderer.invokeElementMethod(this.inputYT.nativeElement, "insertAdjacentHTML", ["beforeend", "<button onclick = 'this.getInput()' type = 'button' class = 'btn btn-success' style = 'position: relative; top: -50px; right: 6px; float: right' >Save</button>"]); 

全碼:

import { Component, Input, Output, ElementRef, ViewChild, Renderer } from "angular2/core"; 
import {Video} from "./video"; 

@Component(
{ 
selector: "playlist", 
templateUrl: "app/ts/playlist.component.html", 
inputs: ["videos"] 
}) 
export class Playlist 
{ 
iframe: HTMLIFrameElement; 
div: HTMLDivElement = document.getElementsByTagName("div")[0]; 
input: HTMLInputElement; 
videoCode: string[]; 
@ViewChild("inputDiv") inputYT: ElementRef; 
constructor(private renderer: Renderer) 
{ 
} 
getInput() 
{ 
    this.videoCode = this.input.value.split("=", 2); 
    this.videoCode = this.input.value.split("&", 2); 
} 
addVideos() 
{ 
    if(this.input) 
     return; 
    this.renderer.invokeElementMethod(this.inputYT.nativeElement, "insertAdjacentHTML", ["beforebegin", "<script src = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>"]); 
    this.input = document.createElement("input"); 
    this.renderer.invokeElementMethod(this.inputYT.nativeElement, "insertAdjacentHTML", ["beforeend", "<input type = 'text' class = 'form-control input-lg' id = 'inputText' style = 'margin: 10px 0px; width: 100%;' placeholder = 'Paste youtube video link here'/>"]); 
    this.renderer.invokeElementMethod(this.inputYT.nativeElement, "insertAdjacentHTML", ["beforeend", "<button onclick = 'this.getInput()' type = 'button' class = 'btn btn-success' style = 'position: relative; top: -50px; right: 6px; float: right'>Save</button>"]); 
} 
onSelect(vid: Video) 
{ 
    if(this.iframe) 
     this.div.removeChild(this.iframe); 
    this.iframe = document.createElement("iframe"); 
    this.iframe.setAttribute("width", "800"); 
    this.iframe.setAttribute("height", "500"); 
    this.iframe.setAttribute("src", "https://www.youtube.com/embed/" + vid.link); 
    this.div.appendChild(this.iframe); 
} 
} 

回答

0

添加onclick事件這樣的 -

(click)="getInput()"