6

我现在正在使用 AngularJS 框架,但偶然发现了一个问题。我做了一个指令,叫做“输入”。mouseenter它在和上触发功能mouseleave。我将它作为属性应用到表格行元素。它现在为每个子元素(所有列等)触发,但它应该只在您将鼠标悬停在表格行上时触发。

这就是我的指令的样子:

myapp.directive('enter', function(){
    return {
        restrict: 'A', // link to attribute... default is A
        link: function (scope, element){
            element.bind('mouseenter',function() {
                console.log('MOUSE ENTER: ' + scope.movie.title);
            });
            element.bind('mouseleave',function() {
                console.log('LEAVE');
            });
        }
    }
});

这是一个例子:http: //jsfiddle.net/dJGfd/1/

您必须打开 Javascript 控制台才能查看日志消息。

在 AngularJS 中实现我想要的功能的最佳方法是什么?如果有合理的 AngularJS 解决方案,我宁愿不使用 jQuery。

4

1 回答 1

6

你可以试试这个:

myapp.directive('enter', function () {
    return {
        restrict: 'A',
        controller: function ($scope, $timeout) {
            // do we have started timeout
            var timeoutStarted = false;

            // pending value of mouse state
            var pendingMouseState = false;

            $scope.changeMouseState = function (newMouseState) {
                // if pending value equals to new value then do nothing
                if (pendingMouseState == newMouseState) {
                    return;
                }
                // otherwise store new value
                pendingMouseState = newMouseState;
                // and start timeout
                startTimer();
            };

            function startTimer() {     
                // if timeout started then do nothing
                if (timeoutStarted) {
                    return;
                }

                // start timeout 10 ms
                $timeout(function () {
                    // reset value of timeoutStarted flag
                    timeoutStarted = false;
                    // apply new value
                    $scope.mouseOver = pendingMouseState;
                }, 10, true);
            }
        },
        link: function (scope, element) {
            //**********************************************
            //  bind to "mouseenter" and "mouseleave" events
            //**********************************************
            element.bind('mouseover', function (event) {
                scope.changeMouseState(true);
            });

            element.bind('mouseleave', function (event) {
                scope.changeMouseState(false);
            });

            //**********************************************
            //  watch value of "mouseOver" variable
            // or you create bindings in markup
            //**********************************************
            scope.$watch("mouseOver", function (value) {
                console.log(value);
            });
        }
    }
});

在http://jsfiddle.net/22WgG/同样的事情

也改为

element.bind("mouseenter", ...);

element.bind("mouseleave", ...);

你可以指定

<tr enter ng-mouseenter="changeMouseState(true)" ng-mouseleave="changeMouseState(false)">...</tr>

http://jsfiddle.net/hwnW3/

于 2013-03-10T23:20:08.233 回答