从angular2 app中的jquery传递来自mouseenter事件的JS变量

时间:2016-12-07 11:44:45

标签: javascript jquery angular

我的巫婆功能有问题:

inactive() { 
        let target = $('.album__item--cover-container');
        var leave:boolean;
        target.mouseleave(function() {
            leave = true;
            console.log("LEAVE MOUSEENTER: ", leave);
        });
        console.log("LEAVE: ", leave);
        if (leave) {
            console.log("LEAVE CONDITION: ", leave);
            this.flyOut = 'inactive';
        }
        console.log(this.flyOut); 
    }

我不知道日志"LEAVE MOUSEENTER"是否属实,退出鼠标事件日志后"LEAVE“是undefined。我无法在此this.flyOut = true中解除var FabricCanvasObject = function(canvasId, attrSet) { this.theCanvas = new fabric.Canvas(canvasId, attrSet); this.addShape = function(shapeAttrSet) { var newShape = this.theCanvas.add(new fabric.Circle(shapeAttrSet)); this.canvasElements.length += 1; this.canvasElements[shapeAttrSet.name + this.canvasElements.length] = newShape; }; this.canvasElements = { length: 0 }; }; var myFabric = new FabricCanvasObject('c', { targetFindTolerance: 15 }); console.log(myFabric); myFabric.addShape({ radius: 20, fill: 'green', left: 100, top: 100, name: 'circle', id: 1 }); myFabric.addShape({ radius: 20, fill: 'red', left: 150, top: 150, name: 'circle', id: 2 }); myFabric.addShape({ radius: 20, fill: 'blue', left: 200, top: 200, name: 'circle', id: 3 }); console.log(myFabric.canvasElements); console.log(myFabric.canvasElements.circle1); 事件功能。请有人帮助我,并解释为什么这种行为会这样?

此致 格雷格

2 个答案:

答案 0 :(得分:1)

我认为你的问题是因为你在鼠标离开事件的回调中没有做任何事情。

仅在调用inactive()时运行一次。

if (leave) {
    console.log("LEAVE CONDITION: ", leave);
    this.flyOut = 'inactive';
}

我假设你试图在mouseleave上设置一些状态,然后你想检查this.flyOut是否处于非活动状态?

(function ($) {// just some wrapper
    let flyOut = 'inactive'
    const target = $('.album__item--cover-container'); // Use const if you know it wont change
    const check = $('.check-button-class-thing');

    target.mouseleave(function() {
        flyOut = 'inactive'
    });

    target.mouseenter(function() {
        flyOut = 'active'
    });

    check.click(function() {
        console.log(inactive()); // Is it inactive or not
    });

    function inactive() {
        return flyOut === 'inactive' ? true : false;
    }
})(jQuery);

我在这里对您的用例做出假设,但我希望这会有所帮助。代码本身不是很有用,因为它取决于.check-button-class-thing嵌套的位置,但它只是一个例子。

答案 1 :(得分:0)

mouseleave

超出inactive()函数回调。因此,当第一次调用leave时,undefined已声明没有任何默认值(它是mouseleave)。 if被称为。 只有在调用TRANSFORM Count(AlphaData.[Invoice]) AS CountOfInvoice SELECT AlphaData.[Reason], Count(AlphaData.[Invoice]) AS [Total Of Invoice] FROM AlphaData WHERE ((AlphaData.[DateRaised]) Between AlphaData.[DateRaised] And AlphaData.[DateRaised]) GROUP BY AlphaData.[Reason] PIVOT Format([DateRaised],"Short Date"); 事件时才会出现这种情况。