简体   繁体   English

使用手势服务时,Angular指令不触发ng-blur

[英]Angular directive not firing ng-blur when using gesture service

I'm trying to create a directive that reveals a popup bubble when you tap and hold on the element. 我正在尝试创建一个指令,当您点击并按住该元素时,该指令会显示一个弹出气泡。 When I use the on-hold directive everything seems to work, but when I use the ionicGesture service I am unable to attach focus to revealed element. 当我使用保留指令时,一切似乎都可以正常工作,但是当我使用ionicGesture服务时,无法将焦点附加到显示的元素上。 Here is my code that works using the on-hold directive: 这是我使用保留指令的代码:

app.directive("copyContent", function ($rootScope, $compile, $document, $timeout) {
    return {
        link: function ($scope, $element, $attr) {
            var popup = "<div class='copy-popup' ng-show='copying'><input type='button' value='Copy'  ng-click='copyItem()' ng-blur='removePopup()' class='button button-dark'></div>";
            var el = $compile( popup )( $scope );
            $element.append( el );
            $scope.$watch('copying', function(newValue, oldValue) {
                $timeout(function () {
                    if (newValue){
                        $element.addClass('copy-select');
                        el.find('input').focus();
                    }
                });
            });
            $scope.removePopup = function(){
                $scope.copying = false;
                $element.removeClass('copy-select');
            };
            $scope.copyItem = function () {
                var copyString = $attr.copyContent;
                console.log(copyString);
                if(cordova.plugins){
                    cordova.plugins.clipboard.copy(copyString);
                }
                $scope.removePopup();
            };
        }
    };
});

and the html: 和html:

<div ng-repeat="comment in comments track by $index" class="message-wrapper">
<div class="chat-bubble right"  copy-content="{{comment}}" on-hold="copying = true"></div>
</div>

The unexpected behavior occurs when I try removing the on-hold directive from the html and instead try using the ionicGesture service within the copy-content directive, here is the code that doesn't work using the ionicGesture on hold service: 当我尝试从html中删除保留指令,而是尝试在copy-content指令中使用ionicGesture服务时,会发生意外行为,这是无法使用ionicGesture保留服务的代码:

app.directive("copyContent", function ($rootScope, $compile, $document, $timeout, $ionicGesture) {
    return {
        link: function ($scope, $element, $attr) {
            var popup = "<div class='copy-popup' ng-show='copying'><input type='button' value='Copy'  ng-click='copyItem()' ng-blur='removePopup()' class='button button-dark'></div>";
            var el = $compile( popup )( $scope );
            $element.append( el );
            $scope.removePopup = function(){
                console.log('blur');
                $scope.copying = false;
                $element.removeClass('copy-select');
            };
            $scope.copyItem = function () {
                var copyString = $attr.copyContent;
                if(cordova.plugins){
                    cordova.plugins.clipboard.copy(copyString);
                }
               $scope.removePopup();
            };
            $ionicGesture.on("hold", function(){
                $scope.copying = true;
                $timeout(function () {
                    console.log('focus');
                    $element.addClass('copy-select');
                    el.find('input').focus();
                });
            }, $element);
        }
    };
});

and the html: 和html:

<div ng-repeat="comment in comments track by $index" class="message-wrapper">
<div class="chat-bubble right"  copy-content="{{comment}}"></div>
</div>

The popup seems to show correctly, but the removePopup function on ng-blur is not fired when clicking outside of the input type=button. 弹出窗口似乎正确显示,但是在输入type = button之外单击时,不会触发ng-blur的removePopup函数。 Why would using the ionicGesture service cause ng-blur to not be fired correctly, and using the on-hold directive work? 为什么使用ionicGesture服务会导致ng-blur无法正确启动,而使用保留指令会起作用?

The listener functions attached via 通过附加的侦听器功能

$ionicGesture.on $ ionicGesture.on

execute outside of Angular's context and don't trigger the digest loop, which means the watcher won't see any changes. 在Angular的上下文之外执行并且不触发摘要循环,这意味着观察者将看不到任何更改。

If you add 如果添加

$scope.$apply(); $ scope。$ apply();

inside the hold function you will notice that the watcher will start seeing the changes. 在保持功能中,您会注意到观察者将开始看到更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM