这个问题类似于那个Call Angular Function with Jquery

但是我认为我的例子差别不大。

Angular项目上,我有一个按钮:

<button id="button1" #tbName title="tbName" class="btn btn-lg btn-outline-primary" (click)="open(content,tbName.title)">Launch demo modal</button>


在TS文件上,有一种方法:

open(content, title) {
    console.log(document.getElementById('button1').getAttribute('title'));
    this.id_desk = document.getElementById('button1').getAttribute('title');
    this.modalService.open(content, { centered: true, container: '#test', ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {

        this.closeResult = `Closed with: ${result}`;
    }, (reason) => {

        this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
}


看起来像直接调用open(content,title)角度方法的jQuery函数?

最佳答案

我强烈建议您学习使用Angular(&w / o JQuery)而不是将两者混合使用。

现在我们在SOF上,您问了一些问题。您的代码有很多错误,但是,嘿,那不是我的项目,所以我就回答。

如果要使用JQuery访问Angular函数,请像这样将其绑定到窗口。

ngOnInit() {
  window['MyComponent']['open'] = this.open.bind(this);
}


现在在JQuery中,您可以使用

$(...).click(function() {
  window.MyComponent.open('content of the modal', 'title of the modal');
});

关于jquery - 如何从jQuery调用Angular方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52098788/

10-12 00:32
查看更多