Retrieving data from a directive in an HTML table

Utilizing AngularJS, I am seeking a method to retrieve the value from an input located within a specific row in a table.

Presented below is an input element. Upon clicking the "add" button, my objective is to extract the value corresponding to that particular row.

View

   <tr ng-click="" ng-repeat='customer in customers | filter:query | orderBy: sort.field : sort.order'>

       <td ng-repeat='field in fields'>
           {{customer[field]}}
       </td>
       <td mult-ecs>
           <p class="input-group">
                <span class="input-group-addon">Rs</span>
                <input class="form-control" name="debit" type="text" ng-model="customer['id'].value">
           </p>
           <button ng-click="addMulEcs(customer['id'])" class="btn btn-primary btn-sm" >Add</button>
           <button ng-click="removeMulEcs(customer[id])" class="btn btn-danger btn-sm" >Remove</button>
        </td>
    </tr>

Controller

.directive('multEcs', ['$http', function($http){
    return{
        restrict: 'A',
        replace:false,
        scope:{
        },
        link: function(scope, elem, attr){
            scope.addMulEcs = function(id){

            }
        }
    }


}]);

Image of Table

Answer №1

To access a variable in the controller scope from within a directive, you can bind it to the directive's scope. In your scenario, using one way binding would be most appropriate.

In your directive's settings:

scope:{
          localEcsValue : "@ecsValue"
    },

Then in your template :

<td mult-ecs ecs-value="customer['id'].value">

This will link scope.localEcsValue to customer['id'].value in the controller.

In the link function, you can use:

scope.addMulEcs = function(id){
            alert( scope.localEcsValue() )
        }

It's important to be cautious when using ng-model within a directive as it creates an isolated scope which may prevent the correct binding of ng-model to the desired variable. For more information, see this resource.

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

Struggle with encoding dropdown menu options

I have multiple forms on my webpage and I want to be able to access them all at once and include them in a single large GET request. It's mostly working, but I'm encountering difficulties when dealing with drop down menus because their structure ...

Is there a way to ensure a Javascript alert appears just one time and never again?

I struggle with Javascript, but I have a specific task that needs to be completed. I am participating in a school competition and need an alert to appear only once throughout the entire project, not just once per browsing session. The alert will inform ...

Open the CSV document

Why am I receiving an error stating ./vacancy-data.csv cannot be found when attempting to pass the csv file into the csvtojson npm package? The csv file is located in the same directory as the code below: var express = require('express'), route ...

What could be causing the npm install command to not save packages in the /node_modules directory?

After running npm install to add a package, npm indicates that the installation was successful. However, upon checking the node_modules folder, I'm unable to locate the installed package. In order to access the package, I have resorted to using npm in ...

Configuring route for serving static files in an Express server

I'm completely new to working with express, but I am eager to learn and follow the best practices. My goal is to serve files like CSS or index.html from a folder called 'public'. I have seen examples using .use and .get methods as shown belo ...

I can't figure out why I keep encountering an injection error while trying to utilize ui.router

Currently, I am in the process of working on my application and trying to follow the 'Getting started' section of the UI Router page to get things up and running smoothly. (function () { var app = angular.module('configurator.boot' ...

What could be causing my div element to remain stationary despite the movement functions I have implemented?

I am attempting to move my div element by using key presses to adjust the CSS attributes of the HTML elements. However, despite implementing the necessary code, the mover refuses to budge and nothing appears when I inspect the elements or their attributes. ...

How can I insert an empty option following a selected value in a dropdown menu using jQuery?

How to use jQuery to insert a blank option after an existing option? I attempted to add a blank option, but it ended up at the top of the dropdown. I actually need one existing option followed by one blank option. For example: <option></option& ...

Unlocking the Power of Large Numbers in Angular with Webpack

Error: [$injector:unpr] Unknown provider: BigNumberProvider Recently, I embarked on a new project using Webpack + Angular.JS and encountered an issue while trying to incorporate Bignumber.js. Here's a snippet of my Webpack configuration: resolv ...

Issue with Electron | JavaScript Runtime

Attempting to utilize WebTorrent with Electron and Node.js for torrent downloading. Here is the code snippet in main.js: const electron = require('electron') const { app, BrowserWindow } = electron const path = require('path') const u ...

Error message displayed: "Unexpected token 'H' when attempting to render Markdown

I've been working with the react markdown library and wanted to share my code: import Markdown from 'react-markdown'; import PreClass from './PreClass'; type MarkdownFormatTextProps = { markdown: string; tagName?: string; ...

The Server Components render encountered a glitch

Screenshot of the errorI am encountering a strange error only in the production environment. The lack of additional information leads me to believe it may be due to security measures put in place for production. Unfortunately, I have been unable to repli ...

Authentication with AngularJS and Java, managing $scope

My current system setup includes: front-end = AngularJS back-end = Java EE7/REST-API Both applications are operating on Wildfly 8.2 using Undertow as the application server. One of my main concerns is about the authentication process: Should I impl ...

Guide to sending multiple forms from an HTML page to another PHP page

What is the best way to submit multiple forms on a single HTML page to a PHP page and process all the form elements in PHP upon clicking one button? Any suggestions are appreciated. Thank you in advance. <form id="form1" name="form1"action="abc.php" ...

Is it necessary to perform a specific action if the text within a div matches pre

I've been struggling to make this work properly. Even after removing the AJAX POST function, the issue persists. No alerts or any indication of what's going wrong. Check out the code on JSFiddle: HTML <div class="notification"> ...

What is the best way to arrange flex elements in distinct columns for stacking?

Recently, I utilized the flex property along with flex-flow: column nowrap to showcase my elements. Take a quick look at what my elements currently resemble: [this] It's quite apparent that simply stacking both columns on top of each other won't ...

Is there a way to determine the model name programmatically within a Sails.js lifecycle callback?

Two models are present, with one model extending from the other. For all sub-models to inherit a lifecycle callback defined in BaseObject, I need a way to retrieve the name of the model being acted upon within the callback. This information is crucial for ...

Angular not executing Jquery script in included HTML files

Underneath is a simple jquery code snippet: $(document).ready(function() { $("h2").click(function(){ alert("Hello world"); }) }); After clicking on any h2 tag in the website, an alert message will display. The code functions properl ...

Implementing tether in webpack: A step-by-step guide

My webpack application, which I am using with Laravel Elixir, includes a 'bootstrap.js' file for initializing all libraries. Here is the content of the file: window._ = require('lodash'); /** * We'll load jQuery and the Bootstra ...

Is there a way to modify the appearance of a block element and transmit a parameter to a PHP function?

It might seem like I'm overthinking things, but I am trying to achieve two goals with the HTML code provided. First, when a user selects an option, I want to toggle the display style of the leaderTable from hidden to visible if it meets certain criter ...