Assign the ng-repeat item to a variable in the controller's scope

Can anyone help me figure out how to pass a particular item from my view to a function in my controller? Here is the code:

The view where I want to pass p.id

<tr ng-repeat=" p in projetsListe">
  <td>{{p.NomProjet}}</td>
  <td>{{calcul()}}</td>
</tr>

Controller function:

 $scope.calcul = function(p) {
     var coutprevu = 0;

     for (var i=0;i<$scope.task.length;i++) {
         if($scope.task[i].projet_id == p.id) {     
             coutprevu += $scope.task[i].CoutParJour * $scope.task[i].TempsPrevu;
         }
    }
    return coutprevu;    
};

Answer №1

If we look at it from your perspective, all you need to do is pass the parameter as shown below:

<tr ng-repeat="p in projetsListe">
    <td>{{calcul(p.id)}}</td>
</tr>

Then, in the controller's function, retrieve it like this:

$scope.calcul = function(id) {
    console.log(id); /* This is where it shows up! */

    var coutprevu = 0;
    for (var i = 0; i < $scope.task.length; i++) {
        if($scope.task[i].projet_id == id) {
            coutprevu += $scope.task[i].CoutParJour * $scope.task[i].TempsPrevu;
        }
    }
    return coutprevu;
}

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

Express is unable to locate the specified property

Here is my controller code snippet: exports.showit = function(req, res){ res.render('showpost', { title: req.post.title, post: req.post }) } In my post model, I have included title and name objects: title: {type : String, default : &apos ...

Ways to determine if your code is written in ES6

After completing a lot of Javascript coding, I just learned about the existence of various JS 'versions' such as ES5 and ES6. My project is now up on Github, and someone advised me to convert my ES6 code to ES5 using Babel. The problem is, I&ap ...

Unable to access socket.io after modifying the application's URL

There's been a lot of discussion surrounding this topic, but most of it doesn't apply to my situation since I am using express 4.16.4 and socket.io 2.2.0. Also, my example is already functional on both localhost and remote hosting. When setting ...

How to automatically insert a comma into numbers as you type in Vue.js

I am trying to insert separators in my numbers as I type, but for some reason it is not working. Sample Code <el-input-number v-model="form.qty" style="width: 100%;" id="test" placeholder="Quantity" controls-position="right" v-on:keyup="handleChange" ...

Angular Persistent States in Angular version 2 and beyond

Currently, I am in the process of transitioning an Angular 1.5.X application to Angular 4. Within my app, I incorporate Angular Ui-Router Sticky State from https://github.com/ui-router/sticky-states to prevent loss of content within my view when navigating ...

Struggling with lag in MaterialUI TextFields? Discover tips for boosting performance with forms containing numerous inputs

Having some trouble with Textfield in my MateriaUI project. The form I created has multiple inputs and it's a bit slow when typing or deleting values within the fields. Interestingly, there is no lag in components with fewer inputs. UPDATE: It appear ...

Obtain the selected node in FancyTree

When a button is clicked, I need to grab the current node that is in focus. In my attempt to achieve this, I utilized the getFocusNode() method within a click event handler like so: function retrieveFocusedNode() { var currentNode = $("#tree").fancy ...

What is the impact of sending a JavaScript request to a Rails method every 15 seconds in terms of efficiency?

I have a method that scans for notifications and initiates a js.erb in response. Here is the JavaScript code triggering this method: setInterval(function () { $.ajax({ url: "http://localhost:3000/checkNotification", type: "GET" }); }, 15000); ...

Using ASP.NET to cleverly include script files based on certain conditions

We are currently developing a CMS project in ASP.NET, utilizing jQuery for our client-side scripting needs. At the moment, our project includes the jquery-1.2.6.js file as a mandatory script file. Additional script files are loaded as needed, based on the ...

Angular JS is facing difficulties in being able to call upon another directive

I'm encountering an issue where including another directive related to the current one results in the following error message Error: [$compile:ctreq] http://errors.angularjs.org/1.2.10/$compile/ctreq?p0=myApp.pagereel&p1=ngTransclude Script.js ...

Enhancing TypeScript Data Objects

I'm looking to expand a data object in TypeScript by introducing new fields. Although I know it's a common practice in JavaScript, I'm struggling to get it to compile without making bar optional in the provided snippet. I am interested in f ...

Display loading spinners for multiple ajax requests

At the moment, I am displaying spinners using the ajax setup method call: (function() { $.ajaxSetup({ beforeSend: showLoader, complete: hideLoader, error: hideLoader }); })(); While this setup is functioning properly, the ...

Guide to looping through a map arraylist in JavaScript

Here is a sample map arraylist. How can I access this arraylist using JavaScript from within pure HTML? And how can I iterate over this list? <%@page import="org.json.simple.JSONObject"%> <%@page import="org.json.simple.JSONArray"%> <% JSON ...

Unique issue: Angular encountering a syntax error exclusively in Internet Explorer browser

Encountered an issue with a JavaScript code snippet within my angular project. It throws a syntax error in IE 11, but runs smoothly in Chrome. Interestingly, this particular function is not even called on the initial page load, yet the error persists. Upo ...

Incorporate a fresh element into an object after its initial creation

Hello, I am looking to create an object in JavaScript that includes an array-object field called "Cities." Within each city entry, there should be information such as the city's name, ID, key, and a District array object containing town data for that ...

Venom Bot encounters issue with sending files, displaying "Error processing files" message

When attempting to send a PDF in the code snippet below, I encountered the following error: app.post('/send-pdf',[ body('number').notEmpty(), ], async (req, res) => { const errors = validationResult(req).formatWith(({ msg }) =&g ...

Navigating through two nested arrays in JavaScript to access an object

Having difficulty extracting the nested car values using JavaScript (lodash). Take a look at the JSON data below: { "cars":[ { "nestedCars":[ { "car":"Truck", "color" ...

Setting an if isset statement below can be achieved by checking if the variable

I have included a javascript function below that is designed to display specific messages once a file finishes uploading: function stopImageUpload(success){ var imagename = <?php echo json_encode($imagename); ?>; var result = '& ...

Ways to reach the scope of transcluded elements

Within my directive element, there is a form that needs to set some scope properties. Here's an example: <trans-dir> <form role='form' class='form-inline'> <div class='form-group'> Se ...

Issue with jQuery ajax in Internet Explorer 6

Hey there, I'm dealing with a strange issue: The success handler in my $.ajax call looks like this: function(data){ alert(data); } Seems pretty straightforward, right? The problem I'm facing is that the data sent by the server is ALW ...