Ways to reset a form within an ng-include section

I am facing an issue with my html template, where a reusable modal is included using ng-included like this:

myPartial.html

... SOME HTML ...
<ng-include src="'form.html'"></ng-include>

modal.html

...
<form>
  <input ng-model="comment.value" />
  <button type="submit" ng-click="save(comment.value)" />
</form>
...

I am trying to figure out the best way to clear the form, especially from the controller. I want to clear the form within my save() method after a successful http call, but due to ng-include creating its own scope, I am unsure how to achieve this.

You can see the problem in action on this plunker: http://plnkr.co/edit/zkxZOZr3f7sHJZfCiuoT?p=preview

If you directly place the contents of form.html into index.html, it works as expected.

Answer №1

You're almost there! Make sure to initialize the comment instance in your controller and update the correct value in save. The behavior you're seeing is known as "prototypal inheritance" and you can learn more about it at Understanding scopes. This concept isn't limited to just AngularJS, but also exists in regular JavaScript.

angular.module('app', []).controller('appcontroller', function($scope) {

  // create comment
  $scope.comment = { value: 'test' };
  $scope.save = function(value) {
    console.log('in here ');

    // Update comment.value instead of just comment.
    $scope.comment.value = null;
  };
});

I made some changes to your plunker demo.

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

Ionic causing delay in updating ngModel value in Angular 2

My ion-searchbar is set up like this: <ion-searchbar [(ngModel)]="searchQuery" (keyup.enter)="search();"></ion-searchbar> In the typescript file, the searchQuery variable is defined as follows: export class SearchPage { searchQuery: string ...

Personalized selection in React with list filtering

I have created a custom dropdown menu and I am facing an issue. When a user selects an element from the dropdown, I want that item to be removed from the list so it does not show up again. Here's how it currently looks when I click on the select for t ...

Having trouble retrieving attributes from Typed React Redux store

After scouring Google, official documentation, and Stack Overflow, I am still struggling to access my Redux store from a React functional component (written in Typescript). Despite following various guides, I keep encountering undefined errors. I can retri ...

Is there a way to trigger a button's onclick function when its value is changed using the onchange event?

Is it possible to have an onclick function run when a radio button within an HTML form is selected? I tried using onclick instead of onchange but it doesn't seem to work with radio buttons. Any suggestions or tips on how to resolve this issue? func ...

Issues with displaying ngAnimate animations

For the past 10 hours, I've been attempting to get my animations to function properly. It seems that they only work when I include the animate.css stylesheet and use the "animated classname". However, when I try to create custom entrance and exit anim ...

What are the steps to implement string interpolation within a template element in Vue.js?

Currently, I am looping through a dictionary that is imported from a .json file and I want to compare an attribute in my props with a value in the dictionary. The first div defines the attribute variable to be one of the values in the dictionary, specifica ...

Vue component fails to react to updates from Vuex

Currently, I am developing a system to facilitate the management of orders at a shipping station. Although I have successfully implemented the initial changes and most of the functionality, I am encountering an issue where one component fails to update ano ...

Configuring Proxy Settings for WebpackDevServer

I need assistance setting up a proxy using WebpackDevServer in my React/Node chrome extension. Currently, my server is running on localhost:4000, and the React frontend is on localhost:5000. When trying to access the route /api/user/ticket using Axios, I ...

Can someone explain why this error code 500 is showing up in my React Native development server's response?

An error occurred: TypeError: Cannot read property 'reduce' of undefined at resolveDependencies (G:\React-Native\Native\chatmat\node_modules\metro\src\DeltaBundler\traverseDependencies.js:411:33) at process ...

Guidance on incorporating a function as a prop in React using TypeScript

I'm currently learning TypeScript with React and ran into an issue. I attempted to pass a function as a property from my App component to a child component named DataForm. However, I encountered the following error: Type '(f: any) => any&ap ...

Confirming the data in every cell across each row of SlickGrid

How can I validate all cells in a SlickGrid? Is there a way to use JavaScript to trigger validation for each cell, ensuring that the user has provided something other than the default value? We have a use case where every cell must be edited by the user w ...

Exploring the differences in jQuery

Here is a snippet of my jQuery function: $(function() { $('.submitButton').click(function(){ $('input').each(function() { if ($(this).attr('min')) { if ($(this).val()<$(this).attr ...

What is the best way to interact with parent elements inside an iframe?

I'm working on a fresh image uploader where I aim to place the file input in an iframe and display the uploaded images on the parent page. How can I achieve access to the parent elements from within the iframe? Any examples would be greatly appreciate ...

Execute JavaScript function only if it is the final invocation

I'm currently experiencing an issue with a Javascript function that updates a bootstrap spinner. The function works well in general, but the problem arises when I click multiple times. I utilize ajax to update the quantity of selected products in the ...

Swapping out the "select file" and "add file" buttons for a single unified button

I'm working on implementing file uploads in ReactJS and I want to streamline my UI by having just one "Upload" button instead of separate "choose file" and "upload" buttons. Here's the code snippet: import React, { useRef } from 'react' ...

Strategies for managing a disruption during the processing of a form submission that is saving data to the database

Currently, I am managing user signups using two endpoints: one for creating a user and another for creating a user_file. The user_file table contains a reference to the user table. The function handling the form submission appears as follows: const onSub ...

Wavy CSS Animation

For assistance with my spinning image, please check out this fiddle: https://jsfiddle.net/dricardo1/9a8p6srb/ Hello! I'm struggling with a spinning image that has a noticeable wobble when rotating. I can't seem to find a solution to make the rot ...

Is there a way to extract a particular value from a JSON URL and transfer it to a JavaScript variable?

I am looking to extract current exchange rates from a JSON URL for implementation in a webpage. Specifically, I want to retrieve a particular exchange rate (such as the US Dollar) and store it in a variable for use within a JavaScript function. Below is a ...

How can I effectively store and compare previous API responses with new ones in a Node.js environment?

I am working on a project where I have a database connected to a microservice API that is accessed by a Node.js server and a React.js UI. The Node.js middleware handles operations to determine which values from the database API are suitable to be displayed ...

Click on an item in the list to remove it from the list

I am working with an object that contains multiple arrays of objects. I need help figuring out how to remove a single item from the list when clicked on. Although I know that using splice can achieve this, I'm unsure about implementing it in this spe ...