Issue with Ionic and Angular: Struggling to empty input field

Within my Ionic app, there is a form that contains an input field and a button. Upon clicking the button, an action should occur within the controller to clear the input field. Unfortunately, this functionality is not working as expected. Despite its simplicity, I am unable to identify what is causing the issue. Any suggestions or advice would be greatly appreciated. I have already explored similar queries on Stack Overflow, Stack Overflow, and Stack Overflow, but none of the solutions provided have resolved the problem. The HTML structure is as follows:

<form novalidate name="customCategoriesForm">
  <ion-list>
    <ion-item>
      <div class="item item-input item-stacked-label noBorder">
      <span class="input-label">Create a new category</span>
      <div class="catRow">
        <input type="text"
              id="newCategoryInput"
              name="addNewCategory" 
              placeholder="Category name" 
              ng-model="addNewCategory"
              ng-minlength="3"
              required
              >
            <button ng-click="addCategory(addNewCategory)"
                    ng-disabled="!customCategoriesForm.addNewCategory.$valid" 
                    class="button button-small button-outline button-positive catButton">Add</button>
      </div>
    </div>
    </ion-item>
    <ion-item>
       ...
    </ion-item>
  </ion-list>
</form>

The JavaScript function involved is as follows:

$scope.addCategory = function (newCategory) {
  $scope.ExistingCategoriesArray.push(newCategory);
  $scope.addNewCategory = "";
}

EDIT:

No visible errors are displayed and no actions are triggered upon button click..

Answer №1

To resolve the issue, simply update $scope.addNewCategory to $scope.addNewCategory.value, as shown below:

The .js:

$scope.addNewCategory = {value : ""}; // initialize an object addNewCategort with the property value = ''

$scope.addCategory = function (newCategory) {
  $scope.ExistingCategoriesArray.push(newCategory);
  $scope.addNewCategory.value = ""; // clear the property value of addNewCategory
}

The html:

<form novalidate name="customCategoriesForm">
  <ion-list>
    <ion-item>
      <div class="item item-input item-stacked-label noBorder">
      <span class="input-label">Create a new category</span>
      <div class="catRow">
        <input type="text"
              id="newCategoryInput"
              name="addNewCategory.value" <!-- Update this line -->
              placeholder="Category name" 
              ng-model="addNewCategory"
              ng-minlength="3"
              required
              >
            <button ng-click="addCategory(addNewCategory.value)" <!-- and update this line -->
                    ng-disabled="!customCategoriesForm.addNewCategory.$valid" 
                    class="button button-small button-outline button-positive catButton">Add</button>
      </div>
    </div>
    </ion-item>
    <ion-item>
       ...
    </ion-item>
  </ion-list>
</form>

Always remember to use a dot in ngModel. Check out this resource for more information.

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

Angular.js Error: SyntaxError - Unexpected token ":"

When using Angular.js to call my Web API, I encountered an error: P01:1 Uncaught SyntaxError: Unexpected token :. In the resources tab, it is indicated that these braces should not be there {"ProductID1":"P01","ProductName1":"Pen","Quantity1":10,"Price1": ...

What methods can I use to evaluate the efficiency of my website?

Is there a way to assess and improve website performance in terms of load time, render time, and overall efficiency? I've heard of YSLOW for firebug, but am curious if there are any other tools or websites available for this purpose. ...

Tips for simulating next/router in vitest for unit testing?

Struggling with creating basic tests for our Next.js application that utilizes the useRouter() hook, encountering errors when using vitest. In search of solutions to mock next/router for unit testing in conjunction with vitest. ...

Angular fails to refresh the display

For a while now, I have been working with Angular without encountering many difficulties. I have a controller that triggers a function on an ng-change event. This particular controller is responsible for generating different reports based on select filters ...

How can I configure a unique error log format in Winston?

I am facing an issue with the default error log format in Winston, as it includes too much unnecessary information such as date,process,memoryUsage,os,trace. How can I remove these unwanted details from the log? logging.js const express = require('e ...

"Reacting to click events, all buttons have been successfully updated in ReactJS

When a button is clicked, all buttons are updated simultaneously. However, I am looking to only change the state of the specific button that is clicked. Please refer to the image links and code provided below. import React from 'react'; import & ...

A guide on how to effectively simulate a commonJS module that has been imported in

How can I successfully mock a commonJS style module in Jest that I am importing for an external connection? The module is called 'ganblib.js' and it's causing some trouble when trying to mock it with Jest. Any advice or suggestions would be ...

Is axios allowed to be used in this scenario?

As a newcomer to web development, I apologize in advance if my question seems basic or if the details provided are insufficient. Nevertheless, I hope you can assist me with the following query: Is it possible to execute an axios.post request within a vue. ...

Order an array in Javascript based on the day of the month

I am trying to create a unique function that changes the order of a string based on the day of the month. For instance, if the input string is "HELLO" and today's date is the 1st of April, the output should be "LHEOL". On the 2nd of April, it should ...

Stopping a requestAnimationFrame recursion/loop: Tips and Tricks

I am developing a game using Three.js with the WebGL renderer that goes into fullscreen mode when a play link is clicked. To handle animations, I utilize the requestAnimationFrame method. The initialization of the animation process looks like this: self. ...

Echarts: Implementing a Custom Function Triggered by Title Click Event

I recently created a bar graph using Echart JS, but I'm struggling to customize the click event on the title bar. I attempted to use triggerEvent, but it only seems to work on statistics rather than the title itself. JSFiddle var myChart = echarts.i ...

How should we provide the search query and options when using fuse.js in an Angular application?

Having previously utilized fuse.js in a JavaScript project, I am now navigating the world of Angular. Despite installing the necessary module for fuse.js, I'm encountering difficulties implementing its search functionality within an Angular environmen ...

Tips for Preventing Unnecessary Ajax Requests

What I require When a click event is triggered, a service call should be made only once. Use case Dropdown1 Dropdown2 Dropdown3 1. There are three dropdowns on the HTML page. 2. When Dropdown1 is called - an AJAX call is made only onc ...

Utilizing ng-route to parse the URL and identify the corresponding controller by its name

I am implementing ng-click and ng-view to navigate my div id= "listings" in order to display various mobile listings including iphone 4, iphone 5, nexus 4, nexus 5, nexus 6, lg g3, and more. You can check out my code on Plunkr. <div ng-repeat = ' ...

Having trouble accessing a JavaScript variable in Javascript due to reading null property 'value'

What I Require I am in need of passing a URL variable from an HTML document to a JavaScript file. HTML Snippet <script> var urlParam = "{{ page_param.asy_request | replace('&','&')}}"; urlParam = urlParam.rep ...

Transforming a collection of items into a JSON format string

UPDATE: There was a mistake in the programming, please refrain from submitting answers. This question will be removed. If you have already submitted an answer, kindly delete it. I am attempting to submit a form using jQuery and ajax. One of the fields con ...

Organizing MVC5 with Angular JS: How to Create an Effective Folder Structure

As someone new to Angular and eager to learn best practices, I'm wondering about the ideal folder structure for an ASP.NET MVC5 AngularJS application. MVC5 default layout - Controllers Folder - Models Folder - Views Folder ---> Action Name Folder ...

What is the reason behind opting for ng-style over style in AngularJS for applying CSS formatting?

Recently, I've delved into AngularJS and have been exploring its depths for a few days now. I'm curious about the use of ng-style in styling components within both static and dynamic webpages. Given that we already have the traditional style tag ...

Verify the presence of blank space with javaScript

I currently have a text box on my website. <input type="text" name="FirstName" value="Mickey"> My goal is to prevent the user from entering empty spaces, tabs, or new lines in the text box. However, I want to allow spaces between characters. Does a ...

Troubleshooting the issue of setting an Array as the scope in AngularJS and encountering

I'm currently utilizing AngularJS. Check out the Fiddle var myArray = [{firstName: 'Jane', lastName: 'Doe', age:29}, {firstName: 'John', lastName: 'Doe', age: 32}]; This is an example of HTML: <body ng-ap ...