Having trouble with ng-click in my AngularJS Restful application

I was attempting to create CRUD operations in AngularJS.

Unfortunately, I am facing an issue where my ng-click is not functioning properly.

Below is the code for my list.html:


  <div class="container">
          <input type="text" ng-controller="Remove">

            <ul>
              <li ng-repeat="person in people | filter: {age: search}" class="row-{{$first}} | orderBy: 'name'">
                {{person.name}} ,  {{person.age}} 
                <a ng-click="edit(person.name)">edit</a> 
                <a ng-click="remove(person.name)">X</a>
                <span ng-show="$first">First</span>
                <span ng-show="$middle">Middle</span>
                <span ng-show="$last">Last</span>
              </li>
            </ul>
</div>

Here is the content of my app.js file:


angular.module('myApp',['ngRoute'])
    .config(function( $routeProvider){
        $routeProvider
          .when('/',{
            templateUrl: 'list.html',
            controller: 'Ctrl'
          })
          .when('/edit/:id',{
            templateUrl: 'edit.html',
            controller: 'Edit'
          })
          .when('/add',{
            templateUrl: 'add.html',
            controller: 'Add'
          })
          .when('/remove',{
            templateUrl: 'remove.html',
            controller: 'Remove'
          })
          .when('/login',{
            templateUrl: 'login.html',
            controller: 'LoginCtrl'
          })
}) 

.run(function($rootScope){
    $rootScope.people =[
      {
        name: "Johna", 
        age: 23
      },
      {
        name: "Shamit",
        age: 74
      },
      {
        name: "Prath",
        age: 20
      },
      {
        name: "Surya",
        age: 28
      },
      {
        name: "Hari",
        age: 13
      }
    ];
})
.controller('Ctrl',['$scope','$rootScope',function(scope,$rootScope){
    scope.addStatus = true;
    scope.name = "Rafal";
    scope.age = "28";
    scope.persons=['Tom', 'Jerry'];
    
    scope.save = function(){
      scope.addStatus = true;
      scope.person = [];
    };
}])
.controller('Edit',['$scope','$rootScope','$routeParams',function(scope,$rootScope,$routeParams){
    scope.edit = function(index){
      console.log("The first edit");
      scope.addStatus = false;
      scope.person = $rootScope.people[$routeParams.id];
    };   
}])
.controller('Add',['$scope','$rootScope',function($scope,$rootScope){
    $scope.add = function(){
      $rootScope.people.push($scope.person);
      console.log($scope.person)
      $scope.person ={};
    };
}])
.controller('Remove',function($scope,$rootScope){
    $scope.people=$rootScope.people;   
    $scope.remove = function(name){
      var index = getSelectedIndex(name);
      alert(index)
      $rootScope.people.splice(index, 1);
    };
})

.controller("LoginCtrl",["$scope",'Auth',function($scope,Auth){

  console.log("heyy")

  $scope.login = function(){

     Auth.signin({
      email:$scope.email,
      password:$scope.password  
    })
  };

   $scope.register = function(){

    Auth.signup({
      email:$scope.email,
      password:$scope.password  
    })

  };

}]);

function getSelectedIndex(name){
    for(var i=0;i<$rootScope.people.length;i++)
        if($rootScope.people[i].name==name)
            console.log(name);
            return i;
    return -1;
};

I am struggling with triggering ng-click and unable to perform edit and delete functions on the page. Any assistance will be greatly appreciated.

Answer №1

In order for "anchor" tags to be clickable, the href attribute is mandatory.

For placeholder links, we can use href="#" or href="javascript:void(0)"

Implement like this:

<a href="javascript:void(0)" ng-click="edit(person.name)">edit</a> 
<a href="javascript:void(0)" ng-click="remove(person.name)">X</a>

It is recommended to use "javascript:void(0)" in this case to avoid issues with ngroute / ui-route.

Additionally, move the $scope.edit and $scope.remove methods to the Ctrl controller.

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 there a way to assign API data as inner HTML using Lit?

Need help setting inner html of html elements with a get request Any suggestions on how to achieve this? import { LitElement, html, css } from "lit"; import { customElement } from "lit/decorators.js"; import axios from "axios" ...

Utilizing Angular to apply multiple ng-repeat directives with multiple filters

I am working on a project that involves multiple ng-repeat lists with several filters. Currently, I am using (ex:A.value) if (ex:B.value), but I would like to implement multiple filters. The filters I want to incorporate are recommend_search, skill_searc ...

Ajax insertion was successful, but the database records are empty

Why is my code able to save data to the database using Ajax, but all rows are empty? Here is My Form: <form name="frm" id="frm" action=""> <div class="form-group"> <label for="namaproduk">Product Name</label> <input t ...

One way to concatenate texts in an array of dictionaries while keeping the order intact in JS/React is to compare and match specific key-value pairs

My dataset looks something like this: Record= [ { "tid":1, token_text": "Canis", "spanid": 1, "label": "Name" }, { "tid":2, "token_text": "Familiaris", "spanid": ...

Having trouble with errors when adding onClick prop conditionally in React and TypeScript

I need to dynamically add an onClick function to my TypeScript React component conditionally: <div onClick={(!disabled && onClick) ?? undefined}>{children}</div> However, I encounter the following error message: Type 'false | (() ...

Jquery failing to append existing values into the DOM

I'm currently attempting to enhance an existing jQuery table structure with the following code snippet: function GetViewData(data) { $.ajax({ type: "GET", url: "/Services/Configuration/ViewServices. ...

Guide on utilizing Vue to trigger an API call when the input box loses focus

I am currently facing challenges while trying to learn vue, and I am not sure how to proceed. I would greatly appreciate any assistance! To begin with, I want to acknowledge that my English may not be perfect, but I will do my best to explain my issue tho ...

What is the optimal parameter order when utilizing pre-curried functions and composition in JavaScript?

We have a simple, mathematically curried function for subtracting numbers: function sub(x) { return function (y) { return x - y; }; }; sub(3)(2); // 1 The function signature matches the obtained result. However, when function composition comes i ...

The equation:() is malfunctioning

Here is a code snippet I'm working with: $("#button").click(function () { for (var i = 0; i < 4; i++) { setTimeout(function () { $(".rows:eq("+i+")").css("background-color", "blue"); ...

Enhance Select Dropdown in AngularJS with Grouping and Default Selection

I am facing an issue with a form that includes a SELECT element. I have successfully loaded the possible values in my controller from a function that retrieves data from a database and groups the options by a group name. Although the list of options is lo ...

Nodejs Websocket integration with the Firefox browser

Currently, I am utilizing Aurora 17 along with Chrome 22 and Firefox 16 in order to develop a basic chat application. My server-side technology is Node 0.8.9. The issue I am experiencing pertains specifically to Firefox, as it fails to establish a connect ...

The exceptional speed of jQuery's each method sets it apart

I'm currently facing an issue where I am unable to successfully add the attribute data-size to my parent div. My code snippet is as follows: var width, height; $(".galerie img").each(function() { width = this.naturalWidth; height = this.naturalH ...

Prevent redundancy by ensuring unique object items are added to an Array

Within my dataset, I have an array of parking objects. Each parking area is structured with 4 floors and each floor has a capacity of 10 spaces for cars. var parking = [ {type:'car',plateNumber:'D555',parkingLevel:L1,parkingNumber:p1 ...

Node.js with Koa: Discontinuation of generator usage on the next step

I am working with a custom object in which I pass a parameter, and then I need to search for all documents in my collection and process them. Here is the code for my custom object: var db = require('../lib/db'); function Widget(n,l,c,o,t,p) { ...

What could be causing the code to malfunction and prevent window.ethereum from working properly?

While attempting to develop a dApp, I have encountered an issue with the browser in Visual Studio Code not recognizing the Ethereum connection, despite having installed MetaMask on the active browser session. Here is a snippet of my code used to test the c ...

Is there a way to asynchronously load image src URLs in Vue.js?

Why is the image URL printing in console but not rendering to src attribute? Is there a way to achieve this using async and await in Vue.js? <div v-for="(data, key) in imgURL" :key="key"> <img :src= "fetchImage(data)" /> </div> The i ...

The ng-disabled directive in AngularJS fails to disable the button as expected

I'm having an issue with setting an input button to disabled when a user selects a specific value from a select box: Here is my select menu: <select id="jobstatus" name="jobstatus" ng-model="associate.JobStatus" class="form-control"> & ...

Steps to extract selected values from the MUI data grid

Can values be retrieved from a mui DataGrid? I have a table and I would like to create a custom export that takes into account user filters and the display status of columns. However, I need to access these filtered values. Is there a way to achieve this ...

At what point should I expect a promise in Protractor to be resolved?

Despite there being similar inquiries on this topic, I am struggling to comprehend them. Allow me to illustrate with an example, where my task is to click a button and verify the URL. Initially, I thought it should be coded as: element(by.id('butto ...

The directive call triggers the loading of data from MongoDB

I'm currently working on a small invoice demo application using Angular. The data is stored in a MongoDB and is called in a controller named invoiceCtrl. Here's how the controller looks: invCon.controller( 'invoiceCtrl', ['$http&a ...