Getting access to a Polymer method inside a function

点点圈 提交于 2019-12-13 06:25:02

问题


I use the Polymer Google Map element and I've faced with a problem while adding an event listener to the map.

    * blah blah blah *
    ready: function () {
        this.map.addEventListener('google-map-rightclick', this._rightClickOnMap);
    },
    test: function () {
        console.log(1);
    },
    _rightClickOnMap: function (event) {
        this.test();
    }
    * blah blah blah *

I've tried to bind this to the listener but JS throw me the error

this.map.addEventListener('google-map-rightclick', this._rightClickOnMap).bind(this);

回答1:


You need to call bind on the function and not on the event listener.

this.map.addEventListener('google-map-rightclick', this._rightClickOnMap.bind(this));


来源:https://stackoverflow.com/questions/37036626/getting-access-to-a-polymer-method-inside-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!