The ng-change and onchange events are not functioning as expected for the input field with the type "file"

<button ng-click="controller.foo()">Click<button>

is functioning properly.

However,

<input type="file" ng-model="logo" onchange="controller.foo()">

seems to be malfunctioning. Additionally,

<input type="file" ng-model="logo" ng-change="controller.foo()">

is also not working as expected.

The error message I receive for the first one that is not working:

product-page4:1 Uncaught ReferenceError: controller is not defined at HTMLInputElement.onchange (product-page4:1)

As for the second one, there is no response at all.

Any suggestions on how to make this function properly for the input element?

Answer №1

In order to interact with the methods of a controller in your view, it is recommended to define them using $scope:

$scope.foo = function () {
  alert("works");
}

You can then call the method from HTML like this:

<input type="file" ng-model="logo" ng-change="foo()">

Although it may be possible with this (I have not tested it, but if I remember correctly...)

Controller:

this.foo = function () {
  alert("works");
}

HTML:

<input type="file" ng-model="logo" ng-change="controller.foo()">

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

Error: The function res.getHeader is not recognized within the Next.js API environment

I am currently working on a web application using NextJS 13, TypeScript, Next Auth v4, Prisma (using SQLite for development), and OpenAI. While accessing the API endpoint, I encountered an error in the console with the following message: error - TypeError ...

I encountered a frustrating issue with saving user input data using localStorage

One of my goals is to store the current inputs even if the user refreshes or closes the browser. The issue I'm facing is that when I select Yes in the radio button, then refresh the page or close the browser and reopen it, the No button is checked wh ...

Ways to ensure that the dropdown content remains visible even after the mouse has exited the trigger element

There are three buttons arranged in a row, and when one is hovered over, dropdown content appears underneath all three buttons. However, the content disappears when the mouse moves down to it. Unfortunately, images cannot be posted inside yet** https://i.s ...

What is the best way to focus automatically after switching the display from none to block?

Within my HTML code, there is a group of input elements nested under another group of div elements. I have a button that, when clicked, toggles the display of one of the input elements from hidden to visible using JavaScript: if (switched) { docum ...

Guide on sending a key to a text input field using JavaScript

How can I simulate sending a combination of keys (such as Ctrl+C or Alt+Shift) when the cursor enters an input text field using Javascript? I am not utilizing jQuery, but rather MS-Ajax. Is it achievable with MS-Ajax DOM? EDIT 1) Following @Ghostoy&apos ...

JavaScript - Capture user input and store it in a cookie when the input field is changed

Any help with my issue would be greatly appreciated. I'm currently working on saving the value of a form input type="text" to a cookie when the input has changed. It seems like I'm close to getting it right, but unfortunately, it's not worki ...

Updating state in React with a nested promise within the useEffect hook

Trying to update the component state using React.useState with the help of useEffect. The reason behind using useEffect is that the API call response (EmployeeState > init()) determines what gets displayed on the UI. Component: import { EmployeeState } ...

Spotting the Visible Element; Detecting the Element in

Currently, my webpage has a fixed header and I am facing the challenge of dynamically changing the styling of the li elements within the navigation based on the user's scrolling position. To achieve this, I believe the most effective approach would b ...

using configureStore instead of createStore

After updating my packages, I received a notification from VScode indicating that the createStore function was deprecated. This prompted me to go ahead and replace it with the recommended alternative. In my store file, I left the original line as a commen ...

The validations continue to function properly even when HTML has been removed

In my form, I have implemented an "addmore" functionality that allows users to dynamically add a set of HTML input fields. Users can also remove the added rows if needed. While everything is functioning correctly, I am encountering an issue where validatio ...

Tips for capturing the interaction between a jQuery Dialog and its Parent

Within the parent HTML, there is a call that triggers a JavaScript function. <div class="data"> <form:input title="Building" styleClass="content contractorDisable" maxlength="5" size="6" path="fireImpairForm.bldCode" />&nbsp; <a hre ...

Tips for passing a URL variable into an Ajax script to prefill a form input in a search field

I have a website with a search feature that dynamically queries a database as you type in the search field, similar to Google's search suggestions. This functionality is achieved through AJAX. As the results appear on the page while you enter your se ...

Unable to retrieve information from the API server (with a public IP) using ngResource

This may seem like a naive question, but I am stuck and new to Angular. Despite searching extensively, I have not been able to find a solution. var app=angular.module('myApp',['ngResource']); app.controller('myCtrl',['$s ...

Customizing particle textures in Three.js Particle System for unique visual effects

I have a scene in Three.js with 1000 particles and I've loaded 2 different textures. var particleCount = 1000; var texture1 = THREE.ImageUtils.loadTexture( 'texture1.png' ); var texture2 = THREE.ImageUtils.loadTexture( 'texture2.png&ap ...

Effortlessly switch between CSS animation styles while adjusting animation settings

My HTML element with animation is defined as follows: <div id="box"></div> The box starts by moving 200 pixels to the right, taking 4 seconds to complete. .anim { animation-name: anim; animation-duration: 4s; animation-t ...

What is the best way to retrieve error messages from a transaction with ethers.js?

Incorporating a custom error in my Solidity contract: error Documents__AlreadyExist(string hash); The modifier I utilize is as follows: modifier alreadyExist(string memory hash) { if (s_documentsHashes[hash]) { revert Documents__AlreadyExi ...

Incorporating tawk.to into a Nuxt/Vue application

Has anyone had success implementing tawk.to in a Nuxt application? I took the initiative to create a file called "tawk.js" in my plugin folder and added the following code: var Tawk_API = Tawk_API || {}, Tawk_LoadStart = new Date() (function () { ...

Angular ngStyle Parsing Issue: Encountered an unexpected symbol [ when an identifier or keyword was expected

Is there a way to pass a dynamic string using the fieldName to retrieve an attribute from the item object without encountering syntax errors in older versions of Angular? Here is a simple example to illustrate the issue. While this code works fine with ang ...

Voice message delivery via AWS Pinpoint failed due to a missing resource

Currently, my Node.js service utilizes AWS Pinpoint to send voice calls. However, I am encountering a Resource not found error message when attempting to make a call using Pinpoint.sendMessages. Interestingly, using PinpointSMSVoice.sendVoiceMessage for th ...

Switching image sources using jQuery on click

Take a look at the code snippet I've assembled below: $('img').on({ 'click': function() { var src = ($(this).attr('src') === 'memes/2a.png') ? 'memes/2b.png' : ...