angular 4-在jquery内部使用更正此问题

angular 4-在jquery内部使用更正此问题,angular,Angular,我在ngOnInit中使用jquery的ui sortable,它工作得很好,但是在更新之后我想调用一个服务方法,但它并不引用组件类,所以如何使它引用组件类呢 $('tbody').sortable({ items: "tr:not('.home')", placeholder: "ui-state-hightlight", update: function () { var ids = $('tbody').sortab

我在ngOnInit中使用jquery的ui sortable,它工作得很好,但是在更新之后我想调用一个服务方法,但它并不引用组件类,所以如何使它引用组件类呢

$('tbody').sortable({

        items: "tr:not('.home')",
        placeholder: "ui-state-hightlight",
        update: function () {
            var ids = $('tbody').sortable("serialize");
            this.pagesService.postReorderPages(ids).subscribe(); // how to make THIS here refer to current component class?
        }

    });
尝试使用而不是函数:

或者,您可以在函数后添加bindthis:


箭头功能好像做的吧,绑定不用,谢谢!当你在这里的时候,你知道为什么这个.pagesService.postReorderPagesids;发送正确的值和this.pagesService.postReorderPagesids.subscriberes=>{console.logres;};发送一个空对象?ids变量在第一个示例中具有正确的值,但在第二个示例中是空对象。@VojislavKovacevic在this.pagesService.postReorderPagesids.subscriberes=>{console.logres;};语句,其中哪个保存空对象,ID还是res?ID。如果在服务方法中,我将ID放在对象中,它会起作用,比如返回以下.http.post'http://localhost:3000/pages/reorder-pages',{ids},我确实得到了正确的值,但是整个过程有点不同,不是有效的json,所以它不起作用。无论如何,我相信仅仅传递ID应该能够以某种方式工作;记录正确的值,但当我在返回this.http.post'http://localhost:3000/pages/reorder-pages',ids.mapres=>res.json;express获取一个空对象。另外,如果不是angular post,而是jQueryAjax post,比如so$.postrl,ids;它很好用。
$('tbody').sortable({

    items: "tr:not('.home')",
    placeholder: "ui-state-hightlight",
    update: () => {
        var ids = $('tbody').sortable("serialize");
        this.pagesService.postReorderPages(ids).subscribe(); // now `this` should refers to the component class
    }

});
$('tbody').sortable({

    items: "tr:not('.home')",
    placeholder: "ui-state-hightlight",
    update: function() {
        var ids = $('tbody').sortable("serialize");
        this.pagesService.postReorderPages(ids).subscribe(); // now `this` should refers to the component class
    }.bind(this)

});