Remove ng-src by using ng-click

Having trouble clearing the ng-src (input file) using ng-click. Looking to solve this with Angular. Tried using angular.element.val('') but it's not working for me. Any other suggestions?

controller.js

 app.imagePreview = false;

$scope.thumbnail = {
    dataUrl: null
};
$scope.fileReaderSupported = window.FileReader != null;
$scope.photoChanged = function(files){
    if (files != null) {
        var file = files[0];
        if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
            $timeout(function() {
                var fileReader = new FileReader();
                fileReader.readAsDataURL(file);
                fileReader.onload = function(e) {
                    $timeout(function(){                            
                        $scope.thumbnail.dataUrl = e.target.result;
                        app.imagePreview = true;
                    });
                }
            });
        }
    }
};
app.clearImage = function(){
    $scope.thumbnail = {
        dataUrl: null
    };  
}

index.html

<div class="form-group">
<div class="input-group">
    <div class="input-group-addon">
        <i class="fa fa-picture-o"></i>
    </div>
    <input type="text" class="form-control" readonly>
    <span class="input-group-btn">        
        <span class="btn btn-default btn-file add-img-upload">
            <a id="clearPreview" type="button" ng-click="product.clearImage()">
                <span class="glyphicon glyphicon-remove"></span> Clear
            </a>
        </span>
        <span class="btn btn-default btn-file add-img-upload"><span class="glyphicon glyphicon-folder-open"></span> Choose 
            <input type="file" name="file" id="imgInp" onchange="angular.element(this).scope().photoChanged(this.files)" ng-model="files">
        </span>
    </span>
</div>
<img ng-if="product.imagePreview" ng-src="{{ thumbnail.dataUrl }}" id='img-upload'/>
</div>

Answer №1

The method called app.clearImage in your controller is conflicting with the usage of product.clearImage in your HTML. To resolve this issue, ensure that both instances are named consistently. Try renaming one of them to match the other.

It seems like you want clearImage to be a method within your controller. Modify your HTML code by removing "product." before clearImage and update the controller code by replacing "app." with "$scope.". These adjustments should resolve the inconsistency.

In your HTML:

And in your controller:

$scope.clearImage = function(){
    $scope.thumbnail = {
        dataUrl: null
    };  
}

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 execution of the Javascript function is not being carried out

Why is alert("Test1") being executed, but alert("Test2") is not running? P.S. I'm currently not utilizing JSON data. <script> $(document).ready(function() { var param = 10; $.getJSON( 'modules/mod_sche ...

Is it possible to achieve a height transition using CSS instead of relying on JavaScript

I created these custom cards using a combination of HTML, CSS, and JavaScript. However, I've noticed that the height transition animation is not as smooth as I'd like it to be, especially on certain pages. Is there a way to achieve this animation ...

How to render an array in VueJS using the v-for directive

Upon receiving the GET request, the JSON response is as follows: [{"key":"COMPLAINT","value":"Complaint"},{"key":"DONATE_ENQ","value":"Donation Enquiry"},{"key":"GENERAL_ENQ","value":"General Enquiry"},{"key":"MEMBERSHIP_ENQ","value":"Membership E ...

Tips for Naming Variables and Functions?

Seeking guidance on naming conventions for variables and functions. Are there any recommended books or articles on this topic? Can you assist me in naming variables and functions in the following scenarios (just examples): 1) A function that verifies if ...

Insufficient allocation - memory overflow in loopback.js

I encountered an issue while trying to fetch large data using loopback.js. The error message I received was: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory <--- Last few GCs ---> 45903 ms: Mark-sweep 1385.6 (14 ...

establish an online repository with a data storage system

As a beginner in web development, my goal is to design a website with a database that can house all archive files. I want users to have the ability to delete existing files and upload new ones directly from the web page. Utilizing HTML, CSS, and JavaScrip ...

Having trouble with dynamic CSS styles not functioning properly in Internet Explorer?

My issue involves dynamic CSS settings not functioning properly on Internet Explorer: Here is the code snippet causing the problem: <div class="i-storage-bar-chart-bar-container"> <div class="i-storage-bar-chart-bar" ...

Using jQuery slider and sortable in combination

I have been experimenting with combining jQuery slider and jQuery sortable on the same HTML page. Check out this jsfiddle solution by ostapische Initially, the slider (which highlights items in the list) works perfectly when the page is first loaded. Howe ...

Enhancing data binding in Angular 2.0 with string interpolation

In my script, I am working with a string that goes like this: {name} is my name. Greeting {sender} Is there a module available in Angular 2.0 that allows me to use something similar to the string.format() function in C#? I understand that it can be achie ...

What is the best way to retrieve the coordinates of highlighted text within input or textarea fields?

I am currently developing a Chrome application that involves injecting JavaScript code into a webpage. Whenever a user selects text, a popup appears next to the selected text. To achieve this, I have implemented the following code to retrieve the coordinat ...

Oops! The program encountered an issue where it couldn't access the "Point" property because it was undefined. This occurred while working with openlayers 3 and jsts on

I am currently working on implementing a buffer function for some features that have been drawn on a map following this particular example. However, I am encountering the following error: ERROR TypeError: Cannot read property 'Point' of undefin ...

Is it possible to dynamically store JavaScript code in a variable and then have JavaScript execute it?

I'm uncertain if my current method is the most appropriate for my goal. Essentially, I am fetching data from a database using an ajax call, and I want this data to populate a JavaScript library where the layout is structured like this: data[ item{ ...

Is there a way for TypeScript to recognize that the potential data types in a union type align with the valid prototypes for a function? Any solutions available?

There seems to be an issue with this (playground link) code snippet: type ReplaceAll2ndArgType = string | ((substring: string, ...args: unknown[]) => string) export async function renderHTMLTemplate( args: Record<string, ReplaceAll2ndArgType> ...

Angular's $q is a powerful tool for handling asynchronous operations, while

I've been struggling for hours trying to figure out how $q works. I'm attempting to use it with a function from the google.maps API, but I just can't seem to grasp it on my own. Maybe I completely misunderstand $q. Can someone help me or pro ...

How to fetch JSON data from a URL in Angular 2

let headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded'); let ep = './data.json'; this.events = this.http .get(ep, { headers: headers }) .map(res => res.json()) .map(({results}: ...

Ways to circumvent ng switch and create a component based on type

In my current code, I have an array called resourceTypes and I am using ngSwitch to create different components/directives based on the TypeName. However, I find this approach cumbersome as I have to update the code every time I add a new resource editor. ...

Best Practices for Utilizing NPM Modules with TypeScript

I am interested in developing an npm module using TypeScript. Can anyone suggest a best practice guide on how to start? Here are my questions: Node.js does not natively support TypeScript, so what is the recommended way to publish an npm module? Shoul ...

How to organize PHP JSON data using JavaScript

I need help sorting PHP JSON data from a multidimensional array using JavaScript or jQuery. Here is my PHP code: $array = array(); $x = 0; while($row = $get_images->fetch_assoc()){ $name = $row['name']; $image_type = $row['image_ ...

Using Vue.JS to integrate custom shapes into my mxGraph library

Currently in the process of developing a flowchart editor using MXGraph, I recently integrated it into my Vue.JS project. To do this, I utilized NPM to install the mxgraph library and incorporated the following code to acquire the necessary references in e ...

Assign the Firebase token to the multipart/form-data in a POST request

I am looking to upload an image to my Google App Engine standard environment backend that utilizes Firebase authentication. Specifically, I want to store the image in the blobstore. On the frontend, I am using AngularJS version 1.x. Although I use the clo ...