Is the file-upload function being triggered twice?

I want to share the code snippet I am working on:

.jade:

div(layout="row")
    div(layout="column", flex)
        label(style="margin-left: 5px") File
        md-button(class="md-raised", ng-click="onUploadClicked(3)") Upload
        input(id="image3", type="file", accept=".pdf", file-upload="uploadFile”, style="display: none;")

.js/controller:

$scope.uploadFile = function(file) {
    console.log("upload file");
}

$scope.onUploadImageClicked = function(position) {
    console.log("on upload image");
    $timeout(function() {
        document.querySelector('#image' + (position)).click();
    }, 100);
};

The issue I am facing is that the log message upload file gets printed twice, while the message on upload image only prints once when I click the Upload button, select a file from the chooser, and press Open.

What could possibly be causing this double firing of the upload file message in my code?

Update:

I attempted to use event.stopPropagation(), but it did not solve the problem.

In my javascript files, I have included just the angular.min.js one.

https://i.sstatic.net/LaViM.png

Answer №1

With the guidance of @AnthonyC, I was able to pinpoint the root cause.

In my Jade file, I discovered that I had duplicated this directive in two separate javascript files:

app.directive('fileUpload', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var onChangeFunc = scope.$eval(attrs.fileUpload);

            element.bind('change', function(){
                onChangeFunc(element[0].files[0]);
            });
        }
    };
}]);

The key takeaway here is to ensure there is only one instance of this directive throughout your entire Javascript codebase!

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

The Material UI theme of a React component is not locally scoped within the Shadow DOM

Introduction I have embarked on a project to develop a Chrome Extension that utilizes a React component through the Content script. The React component I am working with is a toolbar equipped with various sub-tools for users to interact with while browsin ...

Altering the display attribute cancels out any opacity transitions

When using JavaScript to change the opacity of an element with transition: opacity 1s, the expected duration of one second is met, as demonstrated here: onload = e => { var p = document.getElementsByTagName("p")[0]; p.style.opacity = 1; }; p { ...

Struggling at the beginning, a basic angular leaflet directive encounters issues

Currently, I'm in the process of incorporating a straightforward leaflet directive into my angular project. The entire code can be found at https://github.com/pluess/woodstore. class MapDirective { constructor() { this.resctrict = ' ...

Removing buttons from a table row dynamically

Below is how I am adding the Button to Element: (this.sample as any).element.addEventListener("mouseover", function (e) { if ((e.target as HTMLElement).classList.contains("e-rowcell")) { let ele: Element = e.target as Element; let ro ...

Preloading error alert message displayed during AJAX request

When I send an ajax request with a dropdown change, the loader div is shown using "before send". However, the issue is that the loader only displays for the first time, even though the ajax functionality works all the time. If you want to check this issue ...

JavaScript "alternating pattern"

When faced with this code test question, I decided to tackle it with the following approach: function createPattern(){ var result = '' for (var row = 0; row < 10; row++){ for (var col = 0; col < 10; col++){ ...

Is there a way to use jQuery to eliminate divs that contain multiple identical data values?

Is there a way to accomplish this? <div class="chat_timestamp_group" data-date="08-March-2016"></div> <div class="chat_timestamp_group" data-date="08-March-2016"></div> <div class="chat_timestamp_group" data-date="14-March-2016" ...

"Utilizing a null value for a zero in Material-ui's Textfield

I am currently using Formik to handle a Material-Ui TextField in my project. The input value (string) is being converted to a number on input change. One issue I am facing is that when the value zero is passed, it is treated as a false value and renders a ...

Is it possible to validate a template-driven form without using the model-driven approach?

Attempting to validate a template-driven form in Angular without two-way data binding has proved to be challenging. I have successfully implemented validation using [(ngModel)], but running into an error when trying to validate the form without the MODEL p ...

"Upon refreshing the page, the onclick event ceases to

What I'm hoping to achieve: I have a div that is used to display user statistics. When a user is clicked on, the details of that user are loaded into the div using an ajax request. Every 10 seconds, the div refreshes and displays the stats of all use ...

The signup controller encountered a malfunction with illegal arguments: an undefined value and a string

I recently started learning MERN through a tutorial and encountered an issue with my database. I have successfully created a function to send data to the database, but for some reason, I can't see the data in the database. Below is the code snippet: ...

Switch the array's value if the key is a match?

I'm currently facing an issue where my code does not push the object when the key matches. How can I update the value of the key instead when there is a match? this.state.data.concat(items).filter(function (a) { return !this[a.key] && (th ...

Only switch a radio button when the Ajax call results in success

Within an HTML form, I am working with a group of Radio buttons that trigger an Ajax call when the onchange() event is fired. This Ajax call communicates with the server to process the value sent by the call. The response can either be a string of "succes ...

Having difficulty retrieving the value of a form using javascript

I've been struggling to retrieve the value of a form using JavaScript, but for some reason it keeps coming up as undefined, almost as if the input doesn't exist. Here's the HTML snippet: %@ Language="VBScript" CodePage=65001 %> ... Th ...

The React application is being continuously accessed by a node.js server and processed within async functions

Currently, I am utilizing React, MongoDB, Node.js, and Express in my project. The specific scenario I am facing involves the following code snippet within my component: renderWishlist(){ var quantity; var itemID; var tmp; var my ...

Stealing mouse events through Iframe

I've implemented a Javascript scroller class on a website that seamlessly adds a scrollbar to predefined elements with overflowing content. The client now wants to include an iframe in one of these elements to add interactive content easily. Despite m ...

AngularJS anticipates the completion of a lone asynchronous request

Attempting to assign data to the scope after an asynchronous call. There is an array named companies in the factory. factory.getByCategoryId = function (id) { $http.get('http://localhost/campaign?access-token=12345&id=2').then( func ...

Troubleshooting: Issues with JS DOM createElement and appendChild functions

I'm currently working on a project to showcase information about various students on a website. In order to achieve this, I'm trying to generate dynamic profile cards and add them to the webpage. However, I seem to be struggling with the Document ...

Generating an array of objects using key-value pairs

After receiving a JSON object with key-value pairs from a service, my task is to dynamically create objects based on them and group them by one column. Here is an example of the JSON structure: [ { "Group" : "A", "Key" : "Name", "Value" : "John" }, ...

Improve page loading speed by removing JavaScript and CSS that block rendering of above-the-fold content, specifically focusing on Angular JS

Currently, I am working on improving the page speed of my MEAN stack application. My main challenge lies in eliminating render-blocking Javascript and CSS to enhance the loading time. Despite making significant progress, I have hit a roadblock with the con ...