Angular directive fails to trigger ng-blur event with the gesture service

I want to develop a custom directive that displays a popup bubble when the user taps and holds on an element. Everything works fine when I use the on-hold directive, but when I utilize the ionicGesture service, I encounter issues with setting focus on the displayed element. Below is the code snippet that successfully implements the functionality 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:

<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>

When I attempt to remove the on-hold directive from the HTML and instead use the ionicGesture service within the copy-content directive, the following code fails to work as expected:

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:

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

Although the popup appears correctly, the removePopup function triggered by ng-blur does not execute when clicking outside the input type=button. The discrepancy between using the ionicGesture service and the on-hold directive raises questions about why ng-blur behaves differently. Any insights into this issue?

Answer №1

When listener functions are attached using

$ionicGesture.on

they run independently of Angular's context and won't trigger the digest loop, meaning any changes won't be picked up by watchers.

However, if you include

$scope.$apply();

within the hold function, you'll see that the watcher will start detecting the changes.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble copying an iframe from one div to another div?

I'm having trouble transferring an iframe to another div. Here is the code I am using: <script type="text/javascript"> $(document).ready(function () { setInterval(function () { if ($('#d1').html() != $('#d ...

Confirm that the input into the text box consists of three-digit numbers separated by commas

Customers keep entering 5 digit zip codes instead of 3 digit area codes in the telephone area code textbox on my registration form. I need a jQuery or JavaScript function to validate that the entry is in ###,###,###,### format without any limit. Any sugge ...

Navigating from one page to another in Ionic is a crucial aspect of building a smooth user

As I explore transitioning between pages in the IONIC framework, I have developed the following code: <div *ngFor="let list of product"> <img [src] ='list.imge'/> <button ion-button round (click)="Contact()">view</button ...

Can we selectively execute certain tests in Cypress using support/index.js?

I need to selectively run certain tests from a pool of 50 test files located in the integration folder. Specifically, I only want 10 of them to execute. In an attempt to achieve this, I am trying to configure the selection process within the support/index. ...

Switching picture using dropdown menu in javascript/jquery

Having some trouble trying to recreate this example. The initial image shows up, but it doesn't change when I select a different option. Still getting the hang of javascript so any help is appreciated. <html> <head> <script src="/j ...

When using keyBy in Laravel, the response may return an object instead of an array

Currently, I am in the process of developing a project where I am setting up a back-end RESTful API using Laravel and a front-end with angularJS. Initially, from my controller@index, I was returning a simple all()->toArray() like this: A. return Respo ...

Strangely odd actions from a JavaScript timepiece

After answering a question, I encountered a problem with my JavaScript clock that displays the day, time, and date on three lines. To make sure these lines have the same width, I used the BigText plugin, which works well except for one issue. tday=new A ...

There seems to be an issue with the React Hooks edit form where it is not selecting the record to edit. Although the currentId is correct

I have a simple CRUD React Hooks app with an ASP.NET Core Web API. The Courses component displays a list, but when I click on a link to edit a particular course, the form shows up with empty fields. Here is the JSX for the Courses component: import React, ...

Issues arise with selecting text and highlighting it in JavaScript functions

I am currently working on a fun little web application that allows users to select text and highlight it in any color they choose. Here is the progress I have made so far: function replaceSelectedText() { var sel, range; var replacementText, spanT ...

Mongoose, unable to modify array element: "key ...$... cannot include a period."

Currently, I am in the process of developing a node app with mongoose integration. Within my mongoose schema, I have implemented an array of images as objects with properties. The structure resembles: const schemaPeople = new Schema({ name: { type: S ...

Ensuring accuracy in form submissions for the month of February

I have a .net custom validator set up to validate an input date in a form. The validation should check the number of days in February, allowing 28/02/2015 but not 29/02/2015. The C# server validation is functioning correctly with the following code snippe ...

What is the process for transferring ng-model values to a table in Angular?

My goal is to populate a table with JSON data using ng-repeat by clicking a button. I need to input either a first name or last name in order to display the results in the table. Is this the correct JavaScript function for achieving this? JavaScript Funct ...

How to automatically choose the first option in a v-for loop in Vue.js

I have a loop that creates a dropdown menu from an array: <select class="form-control" v-model="compare_version" > <option v-for="(version, index) in allVersions" v-bind:value="index" v-bind: ...

unable to call the anonymous inline onclick method of JavaScript

In my HTML file, I have the following code: <a onclick="getRestaurantTime({{ $afternoonTiming[$j]->id}});">Hello</a> Furthermore, I have an anonymous function in restaurant.js file: var Restaurant = (function ($) { function getRestaur ...

What is the best way to extract a numerical value from a prompt dialog box?

When I was attempting to perform basic mathematical calculations using HTML, jQuery, and JavaScript, I encountered an issue with user input. I used the following code to get input from the user: var x = prompt("Enter a Value","0"); var y = prompt("Enter a ...

Accessing the FB Page plugin directly within the app browser

Utilizing the Facebook page plugin in a hybrid app created with worklight (6.2.01) looks like this: <div id="facebook-toc-feed" className="toc-feed"> <iframe id="fb-feed" src={"https://www.facebook.com/plugin ...

What is the most efficient way to identify the top n instances of a specific value within an array using Typescript or JavaScript?

Working with TypeScript, I am dealing with an array of objects that may contain the same values as other objects within the array. For example, the following array consists of objects with the value "intent". My goal is to identify the top 3 most commonly ...

Creating an HTML return statement to specifically target and extract a certain string while disregarding additional data

Looking for a solution to trim a long string returned by a webpage, while ensuring essential data is not removed. Counting characters is not feasible due to the varying length of the return. The return format is as follows: GET /New%20Messenger&subti ...

Adapt the CSS based on different screen sizes

I am currently developing a mobile application using angularjs and the ionic framework. My application is intended for both tablet and mobile versions. I have encountered an issue where I cannot use the same CSS for both the tablet and mobile versions. Is ...

I am hoping for the outcome to be directed to the homepage

I'm struggling to figure this out, as I am new to classic ASP and JavaScript. I hope someone can help me understand. I want to display the response.write on the main.asp (or the result) page, but each time I try, it redirects to pass.asp on a differen ...