"Using AngularJS to clear input fields and update the scope upon clicking with ng

1. Custom Directive

app.directive('inputField', function() {
        return {
            restrict: 'E',
            require: 'ngModel',
            scope: {
                words: '=ngModel'
            },
            transclude: true,

            template: "<input type='text' ng-model='words' placeholder='Translate' />"
        };
    });
    

2. ng-click Function

$scope.clear = function() {
        $scope.words = { word: '' };
    };
    

3. View Structure

<input-field id='inputWord' value='' name="data1" ng-model="words.word"></input-field>
    

Issue After Clearing

{{words.word}}

and the input value still remains, causing a broken $scope.

Seeking advice on clearing all inputs within a ng-repeat and updating the scope accordingly.

Answer №1

Give this a shot.

var app = angular.module('app',[])
app.controller('ctrl',function($scope){
  
  $scope.words = [ {word : 'input1'}, {word : 'input2'}, {word : 'input3'}];
  $scope.clear = function() {
          $scope.words =[ {word : ''}, {word : ''}, {word : ''}];
        };
});
app.directive('inputField', function() {
        return {
            restrict: 'E',
            require: 'ngModel',
            scope: {
                words: '=ngModel'
            },
            transclude: true,

            template: "<input type='text' ng-model='words' placeholder='Translate' />"
        };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <div ng-repeat="word in words">
  <input-field id='inputWord'  name="data1" ng-model="word.word"></input-field>
    </div>
  <button ng-click="clear()">Clear</button>
</div>

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

Having trouble getting MongoDB to connect correctly to my server (using node.js and express), any help would be greatly appreciated

Whenever I make an API request using Hoppscotch, I encounter a terminal error along with status code 500 on Hoppscotch. Despite my efforts to fix the issue, it persists. Below is the code snippet that I am currently working with: var mongodb = require(&apo ...

AngularJS consistently retrieves files regardless of the browser configurations

I'm facing an issue with my NodeJS application and AngularJS frontend. When a user requests a file, it is streamed from the node application to the angular frontend. However, the frontend always downloads the file, regardless of browser settings (e.g. ...

Refresh the page without reloading to update the value of a dynamically created object

Maybe this question seems silly (but remember, there are no stupid questions).. but here it goes. Let me explain what I'm working on: 1) The user logs into a page and the first thing that happens is that a list of objects from a MySQL database is fet ...

Retrieve the report information and present it in a HTML data table using a REST API

My understanding of REST API and Javascript is limited, but I now find myself needing to interact with a third-party company's REST API for email reporting purposes. The data can be accessed through a GET method using a URL with a specific TOKEN: {pl ...

Utilizing array iteration to display images

I am having trouble getting the images to display on my card component. The description appears fine, but the images are not rendering properly even though I have the image data in an array. Here is the Card Component code: export const Card = (props) =&g ...

What solutions are available to improve the clarity of content affected by CSS transforms?

Currently, I am in the process of creating a contact form using CSS and JavaScript. While searching for inspiration, I came across a great example on CodePen. However, I noticed that the contact form in the example is appearing blurry. I attempted to resol ...

What is the best way to integrate AJAX with a confirmation button?

Hey there, I'm currently working on implementing an AJAX URL in a confirmation button to update something in the database. In the JavaScript section, I have the following code: function freeze_account() { $.confirm({ title: 'Confirm ...

"Exploring the Power of Vue 3 Event Bus Through the Composition API

I recently set up mitt and I'm facing difficulties dispatching events to another component. The issue arises due to the absence of this in the setup() method, making it challenging to access the app instance. Here's my current approach: import A ...

Replacing text using regex results in whitespace

How can I eliminate text and spaces? For instance: http://www.exampleweb.com/myprogram.rar http://www.examplebackup.com/mybackups.rar http://www.exampleweb.com/myprogram1.rar I have used something similar to remove the second line: http://www.exampleba ...

Jquery refuses to conceal a particular element

I've encountered this issue before, but I'm facing a problem this time. I'm attempting to create a popup that is initially hidden. When clicked, the popup does appear, but I'm unable to close it. Additionally, when trying to change the ...

A step-by-step guide on creating JavaScript web resources programmatically in a proper

I'm relatively new to CRM, so please bear with me as I may make some mistakes along the way. Currently, I am attempting to dynamically generate a web resource (specifically in JavaScript or JScript) early bound using OrganizationServiceproxy in the f ...

Ignore one specific file when importing all files in Angular 7

In my Angular 7 project, I am utilizing C3 and importing all the necessary files at the beginning of my .ts component file using a wildcard. import * as c3 from 'c3'; While this method works well overall, I encountered an issue where my CSS ove ...

There seems to be an issue with accessing the Angular module, even though it has been clearly

While attempting to execute the code below, two errors are encountered: Error: [$injector:nomod] Module 'rooms' is not available! The module name is spelled correctly and loaded dependencies have been included. The modules are structured in the c ...

Controlling JavaScript Text Opacity on Scroll: Smooth Transitions from Invisible to Visible to Hidden

I am attempting to replicate the Apple Airpods page, including its animation. I have successfully implemented the animation and now I am working on rendering text while scrolling. The text appears correctly but I'm facing an issue with opacity transit ...

Changing the Color of Material-UI Slider: A Step-by-Step Guide

Looking to update the color of Material UI Slider component I have attempted to adjust the CSS styling without success, followed by trying the solution provided in this issue and applying the following code, but the changes are not being reflected: getMu ...

The transition effect is not activated when using the MUI Drawer with react-router-dom

My goal was to merge the persistent drawer similar to the example on the MUI website with react-router-dom. However, I encountered a problem where the transition effect no longer seems to work properly. The paragraph does not resize to accommodate the open ...

Functionality of the Parameters Object

As I transition from using the params hash in Rails to learning Node/Express, I find myself confused about how it all works. The Express.js documentation provides some insight: 'This property is an array containing properties mapped to the named rout ...

Having issues with socket.io connectivity on my Node server

I am setting up my node server to update all connected clients with real-time information. However, when I run the code provided below, the io.sockets.on('connection') callback keeps firing constantly, flooding the console with the message Client ...

Troubleshooting Problems with Fancybox Scaling on Small Landscape-Oriented Mobile Devices

Issue Description: In my responsive web design, I am utilizing Fancybox v2.1.5. The problem arises when smaller mobile devices with a more rectangular shape than square are used. In portrait orientation, when invoking Fancybox, it expands to the width of ...

What is the root cause behind the recurring appearance of this line in Angular/D3.js?

I recently came across an excellent tutorial on integrating the D3.js library with AngularJS by following this link: . The guide provided has been extremely helpful in getting me started (big thanks to Brian!) However, I'm eager to delve deeper into ...