clear form input upon click event in AngularJS

Description:
I am incorporating ui-router to load form pages upon clicking an icon. Every time the user clicks on the icon, a new form should load with all fields reset. By adding ng-click to the icon, we can easily reset the form values.

index.html

<td>
    <a  ui-sref="form2"
    title="yahoo">
    <span class="glyphicon glyphicon-file" ng-click="newForm('new')" id="yahooIcon" ></span>
    </a>
</td>

form2.html

<input type="text" name="firstname" ng-model="myModel.firstname"><br>

app.js

$scope.newForm = function (id){
if ( id == 'new' )
  {
   console.log(" model value inside: "+$scope.myModel.firstname);   
   $scope.myModel = {};
  }
}

Issue:

  1. The data bound to ng-model is displaying as undefined in the controller and unable to reset the model when the icon is clicked.
  2. In the demo, the same controller is utilized. However, if each form has its own controller apart from the index page's controller, how can the button click (ng-click) value be passed to child controllers?

DEMO ( Plunker )

Answer №1

Give this a shot

//On Your Website
<button type="reset" ng-click="clearForm(formObject)">Reset Form</button>

//Using AngularJS
$scope.clearForm = function(form) {
      angular.copy({}, form);
    }

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

Is the memory usage of node.js proportional to the number of concurrent requests, or is there a potential memory leak?

Running the following node.js code: var http = require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type': 'text/plain'}); res.write("Hello"); res.end(); }).listen(8888); Upon starting the server ...

Utilizing negative values in lineTo, strokeRect, and fillRect functions

Is it possible to input negative numbers into the drawing primitives of HTML5 canvas? For instance, if I apply a translation of (100,100), would I be able to draw a rectangle or line with coordinates (-25,-25)? Initial testing using lineTo seems inconclus ...

Binding arguments to child functions within Vue components

When working with React, it's a common practice to bind parameters for child components in the following manner: <Child onChange={e => doThing(complex.variable.inParentScope[3], e.target.value)} foo="bar" /> In Vue, I want to ach ...

What is the reason behind the lack of data sharing among controllers in my service?

I have created a factory to retrieve data from the Database and pass it to all controllers in my application, like so: (function () { angular.module('appContacts') .factory('dataService', ['$http', dataService]); ...

Creating a website that adapts based on the subdomain used

Allow me to clarify my issue as best I can. If you need more details, please don't hesitate to ask, and forgive any errors in English as it is not my native language. Main Objective I am managing a website, www.mywebsite.com, that I intend to use fo ...

Experimenting with an Angular Controller that invokes a service and receives a promise as a

I am currently in the process of testing an angular controller that relies on a service with a method that returns a promise. I have created a jasmine spy object to simulate the service and its promise-returning method. However, my mock promise is not retu ...

Utilizing Regular Expressions and the Quill JS library to highlight a list of prohibited words in bold font?

I'm currently working on developing a straightforward webpage where the text submitted to the server is scanned against a list of prohibited words. If any forbidden words are detected, they should be highlighted in bold. Due to formatting restrictions ...

Is the Router.refresh() function failing to refresh within the next 13 seconds?

'use client'; import { useRouter } from "next/navigation"; export default function Todo({ todo }) { const router = useRouter(); return ( <> <li key={todo.id}> <input type=&apo ...

Is it possible to generate output using multiple screenshots extracted from a JSON file, such as the iTunes Search API?

Is there a way to create a loop and display one or more jpg screenshot URLs from the JSON variable "screenshotUrls"? $.getJSON("https://itunes.apple.com/lookup?id=343200656&callback=?", function (data) { var icon = document.getElementBy ...

Checking and merging arrays in Javascript

I have a unique challenge involving two arrays of objects that I need to merge while excluding any duplicates. Specifically, if an object with the key apple: 222 already exists in the first array, it should be excluded from the second array. Please see be ...

When you try to import a component, it may result in a blank page displaying an error message stating: "TypeError: Cannot set property 'components'

One interesting feature is the Vue component named DisplayContent, <template> <div></div> </template> <script lang="ts"> import { Vue, Component } from "vue-property-decorator"; import Home from "@/ ...

Convert string IDs from a JSON object to numerical IDs in JavaScript

My goal is to convert the IDs in a JSON object received from PHP into numeric keys using JavaScript. The initial structure of my JSON object looks like this: let foo = {"66":"test","65":"footest"}; What I aim for is to transform it into this format: let f ...

"Enhance your website design with a full-width element using Boostrap that align

Whenever my div is scrolled to, it becomes fixed. At that point, I want the div to expand to full width. I achieved this by adding width: 100% to the div. However, the issue is that I want the content of the div to align with the rest of the page content, ...

Error Uploading File to Cloudinary Platform

I am currently developing a REST API using Express.js. The main functionality of the API involves accepting a video file from the client and uploading it to Cloudinary. Interestingly, when I test the API by returning the file back to the client, everything ...

Pop-up message on card selection using JavaScript, CSS, and PHP

I have a total of 6 cards displayed in my HTML. Each card, when clicked, should trigger a modal window to pop up (with additional information corresponding to that specific card). After spending a day searching for a solution online, I've come here s ...

HighCharts.js - Customizing Label Colors Dynamically

Can the label color change along with gauge color changes? You can view my js fiddle here to see the current setup and the desired requirement: http://jsfiddle.net/e76o9otk/735/ dataLabels: { format: '<div style="margin-top: -15.5px; ...

Steps to convert the value from False to True with just a simple press of a

I'm currently learning React.js and I wanted to create this specific application. My goal is to figure out how to toggle between False and True using action buttons. Whether you can provide an explanation or show me an example, it would be greatly app ...

Executing several conditional calls in RxJS

I am currently facing an issue with conditional calls in RxJS. The situation involves multiple HTTP calls within a forkJoin. However, there are dependencies present - for example, the second call should only be made if a boolean value from the first call i ...

Express.io is encountering an issue where it is unable to read the property 'expires' of an undefined element

I am new to working with node.js and I am attempting to utilize express.io for saving cookies. Following a tutorial at https://github.com/techpines/express.io/tree/master/examples#sessions, I have encountered the following error: root@server1 [/nodeexamp ...

The video tag displaying input from the camera functions properly in Safari on iOS, however it does not work in Chrome or any in-app browser

While the video streaming on Android and Safari in iOS is functioning perfectly, issues arise when attempting to use Chrome on iOS or an In-app Browser. The implementation involves Vue as a Single File Component. The HTML code utilized is as shown below: ...