Failure to Reach AngularJS ng-click Function

When trying to add a new product to the product list, I am facing an issue. The products load correctly but the ng-click function is not being triggered. (The alert I set up in the addProduct function is not appearing).

HTML

<div ng-controller="ProductController as ProductCtrl">
Search <input type="text" ng-model="search" placeholder="Search" />
<div>
    Filter Type
    <button ng-repeat="cat in categories" ng-click="filterProductsByCategory(cat)">{{cat}}</button>
</div>
<table cellpadding="5" cellspacing="0" border="1">
    <tr>
        <th>ID</th>
        <th>Product</th>
        <th>Type</th>
        <th>Price</th>
        <th>Add</th>
    </tr>
    <tr ng-repeat="product in ProductCtrl.products">
        <td>{{product.id}}</td>
        <td>{{product.name}}</td>
        <td>{{product.type}}</td>
        <td>{{product.price}}</td>
        <td></td>
    </tr>
    <tr><td></td>
        <td><input type="text" name="newProduct.name" ng-model="productCtrl.newProduct.name"></td>
        <td><input type="text" name="newProduct.price" ng-model="productCtrl.newProduct.price"></td>
        <td><input type="text" name="newProduct.type" ng-model="productCtrl.newProduct.type"></td>
        <td><a href ng-click="productCtrl.addProduct()">Add Product {{productCtrl.newProduct.name}}</a></td></tr>
</table>

Any assistance would be greatly appreciated.

The controller:

app.controller('ProductController', function(productService) {

    var that = this;

    productService.getProducts().success(function(data) {
        that.products = data;
    });

    this.newProduct = "";
    this.addProduct = function() {

        that.products.push(this.newProduct);
        window.alert(that.products);
        this.newProduct = "";

    };
});

Answer №1

There appears to be a typo in your code. The correct controller alias name should be ProductCtrl instead of productCtrl. In addition, make sure to update your ng-model attributes accordingly.

Simply replacing productCtrl with ProductCtrl should resolve the issue.

<tr>
  <td>
     <input type="text" name="newProduct.name" ng-model="ProductCtrl.newProduct.name"/>
  </td>
  <td>
    <input type="text" name="newProduct.price" ng-model="ProductCtrl.newProduct.price"/>
  </td>
  <td>
     <input type="text" name="newProduct.type" ng-model="ProductCtrl.newProduct.type"/>
  </td>
  <td>
    <a href ng-click="ProductCtrl.addProduct()">
       Add {{ProductCtrl.newProduct.name}} to cart
    </a>
  </td>
</tr>

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

I am experiencing issues with the middleware not functioning properly after implementing a custom application with Next.js

I'm currently diving into Next.js version 13 and attempting to customize the app based on the standard documentation. However, it seems that the middleware isn't being invoked as expected. I suspect there might be something wrong with my implemen ...

Error: The reference property 'refs' is undefined and cannot be read - Next.js and React Application

Here is my code for the index page file, located at /pages/index.js import { showFlyout, Flyout } from '../components/flyout' export default class Home extends React.Component { constructor(props) { super(props); this.state = {}; } ...

The modal in Angular UI Bootstrap is returning an undefined result

Currently, I am in the process of developing an app that involves loading a modal upon clicking a button within an ng-grid row. The modal is correctly displayed with all the necessary data, but unfortunately, I am facing an issue when attempting to retriev ...

Creating a loop in a column-based carousel without using JavaScript

I'm currently working on developing a carousel using JavaScript or jQuery. I've attempted the code snippet below, but I'm having trouble getting it to display in 3 or 4 columns. While I understand that Bootstrap can handle this easily, I&apo ...

Stop the loop in cypress

We have a certain situation as outlined below loop through all name elements on the webpage if(name.text() matches expName) { name.click() break out of the loop } else { createName() } How can I achieve this in Cypress? Using return false doesn't se ...

Why bother with Jade or Handlebars when developing AngularJs applications?

I am fairly new to the world of full stack JavaScript applications, and I'm just starting out with Angular. Can someone help clarify things for me? Why should I consider using a templating framework like Jade or Handlebars when developing client-side ...

Include the aria-labelledby attribute in the jQuery Datatable Pagination

Check out the HTML output created by the Jquery Datatable plug-in for pagination(https://datatables.net/plug-ins/pagination/full_numbers_no_ellipses): <div class="dataTables_paginate paging_full_numbers" id="my-table_paginate" aria-l ...

Extracting data from websites using Node.js

Currently, I am tackling a task involving web scraping. To give you some context, I take the URL from my webpage and extract the content located between the <body> tags. My objective is to then display this extracted content on my website. Through my ...

displaying and concealing elements with jquery

Is there a way to hide a div if the screen size exceeds 700px, and only show it when the screen size is less than 700px? Below is the jQuery code I'm attempting to use: jQuery(document).ready(function() { if ((screen.width>701)) { $(" ...

conceal parent window element upon clicking an image within an iframe

I'm facing a challenge with hiding certain buttons in a parent window when opening a modal by clicking an image inside an iframe. Below is the code snippet that I am using: In the parent window - <iframe id="gallery" src="links/gallery.html" wid ...

JavaScript, keep it easy: globalize

Looking for help with a simple regex expression... document.getElementById('testing').value=f2.innerHTML.replace(">",">\n"); I'm having an issue where it only stops after the first line break. How can I make it work for the enti ...

What is the proper way to integrate three.js (a third-party library) into the view controller of an SAPUI5 application

Seeking a Solution Is there a way to integrate the three.js library into SAPUI5 in order to access it using THREE as the root variable in my main view controller? I attempted to create a directory named libs within my project folder and include it in the ...

Seeking assistance in comprehending the method for styling table data using inline CSS in a JavaScript file

I am currently using a JavaScript file that was created for me to update form data based on user selections and display radio buttons along with the corresponding costs. Although the inline CSS in this script works perfectly fine in Chrome and Firefox, it ...

The bot believes it's jamming out to some tunes, but there's nothing but silence in the realm

Essentially, a slash command is utilized to initiate playing a music video in Distube. It plays for approximately 30 seconds and then abruptly stops, even though the queue indicates that the music is still playing. There are no errors appearing in my termi ...

Is it possible to change the name of a PHP file using the value entered in an input

I am currently in the process of trying to change the name of a file while uploading it using the JQuery uploader. I have made some progress, and here is the crucial part of my UploadHandler.php: protected function handle_file_upload($uploaded_file, $name ...

Enhancing ng-model by clicking on a list item

I currently have a textbox on my webpage. <input ng-model="meetingData.client.ClientName" id='txtClient' placeholder="Add new client" type="text"> along with a list of items that I want to interact with. Is there a ...

The Bootstrap modal will not appear when the parent container's position is fixed

I am having an issue with a bootstrap modal that I am using as an instruction guide. My goal is to keep it fixed at the top-right corner of the viewport, so I tried using the fixed position. However, when I do this, the modal turns grey. On the other han ...

Creating an Angular table using reactive forms: a step-by-step guide

After reviewing the HTML snippet provided below, it is evident that there is a table with looping through mat cell using *matCellDef="let model". Inside each cell, there are input fields which are reactive forms. Each row or cell needs to have it ...

Attempting to employ jQuery to generate a text input that allows for inputting multiple incorrect answers

I am putting together a webpage for a friend who has allergies. The idea is that users can input a food item, and the page will indicate whether or not my friend is allergic to it. I have compiled an array of his food allergies, and the goal is for the pag ...

Load a file once the Jest environment has been properly deconstructed

I am currently working on developing a straightforward API using Express, and I have encountered an issue while attempting to add tests with Jest. When running the tests, an error is displayed: ReferenceError: You are trying to `import` a file after the Je ...