在invokeelementmethod()和insertadjacentHTML()中使用onclick事件的调用方法

时间:2017-06-09 02:19:27

标签: angular typescript onclick

我尝试通过onclick事件在保存按钮中调用 getInput()方法。但是这会产生以下错误:

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);
}
}

1 个答案:

答案 0 :(得分:0)

像这样添加onclick事件 -

(click)="getInput ()"