How can I call a service within an anchor tag using ng-repeat and ng-switch?

Within my Single Page Application (SPA), I have implemented some intricate code. The initial step involves making a call to a service to retrieve data and binding it to a scope object. Below is an example of the code:

    $scope.tables.outgoingCommunication = {
        columns: OutgoingCommunicationModel.columns,
        rows: []
    };
    $scope.getOutgoingDocs = () => {
        myService.GetPrintHistory($scope.item.ItemId, $scope.item.ItemDesc, $scope.oDocAge).success((response) => {
            $scope.tables.outgoingCommunication.rows = response.Response.body.Value;
        });
    };

In the HTML markup, the following code is used to bind the outgoingCommunication table:

<section ng-if="tables.outgoingCommunication.rows.length" table-directive="outgoingCommunication" rows="tables.outgoingCommunication.rows" columns="tables.outgoingCommunication.columns" sort-field="'DatePosted'" descending="true" parent-method="toggleAge()" table-template="Templates/app/items/Outgoing-Communication.html"></section>

The template includes the following structure:

I am fetching data from GetPrintHistory with JSON format as shown below:

{
FormNumber: "1060",
FormDescription: "Invoice",
PrintProcessId: 6440187,
DatePosted: "2014-12-20T00:00:00",
PrintXMLId: 5286992,
ItemImageNum: 26
}

A collection of these items is returned.

The model definition is as follows:

 define(["require", "exports"], function (require, exports) {
    var OutgoingCommunicationModel;
    (function (OutgoingCommunicationModel) {
        OutgoingCommunicationModel.columns = function () {
            return [
        {
            field: "DatePosted",
            title: "Date Posted",
            columnSize: "col-sm-3",
            allowTruncate: true,
            sortable: true
        },
        {
            field: "FormDescription",
            title: "Description",
            columnSize: "col-sm-3",
            allowTruncate: false,
            sortable: true
        },
        {
            field: "Actions",
            columnSize: "col-sm-3",
            allowTruncate: false,
            sortable: false
        }
            ];
        };
    })(OutgoingCommunicationModel|| (OutgoingCommunicationModel= {}));
    return OutgoingCommunicationModel;
});

Currently, the FormDescription column and DatePosted field are displayed correctly. However, there is an issue with the Actions column. I aim to configure the anchor link to invoke a service method defined as follows:

    this.GetOutgoingDocument = function (itemId, itemImageNumber, printProcessId, printXmlId) {
        return _this.$http({
            method: "GET",
            url: "api/item/GetOutgoingDocument",
            params: { itemId: itemId, itemImageNumber: itemImageNumber, printProcessId: printProcessId, printXmlId: printXmlId }
        });
    };

I am struggling to integrate the call within the ng-repeat loop using ng-switch-when for the action column. If necessary, I can create a sample on CodePen or a similar platform. Any assistance or guidance would be greatly appreciated.

Answer №1

Have you experimented with utilizing ng-init?

<div ng-switch-when="Actions">
      <a href="#" ng-init="GetOutgoingDocument(....)">View&nbsp;<i class="icon icon-lg icon-file-o"></i> </a>
</div>

If you prefer the service to be called when switching, this method could be effective. Alternatively, if you want the service to be triggered by a button click, you can use ng-click = "GetOutgoingDocument()"

This method assumes that you have GetOutgoingDocument defined on the $scope.

Answer №2

I had to confront this situation in a few different ways.

  1. The table directive I was using had an isolated scope, so I added a scope variable externalCall: "&". I set up my section tag to call this directive like this:

    external-call="selectRow(itemId,itemImageNum, printProcessId, printXMLId)"
    . Then, in my anchor code, I implemented it like this:
    <a ng-click="externalCall({itemId: row.ItemId, itemImageNum: row.ItemImageNum, printProcessId: row.PrintProcessId, printXMLId: row.PrintXMLId})">View&nbsp;<i class="icon icon-lg icon-file-o"></i> </a>

  2. In the code snippet above, I found that the row variable contained all the necessary data from the returned row.

Ultimately, I had to take a slightly different approach. Instead of returning a PDF from the Web API call, I switched to using ng-href to access the API directly.

<a target="_blank" ng-href="api/policy/GetOutgoingDocument?itemId={{row.ItemId}}&itemImageNum={{row.ItemImageNum}}&printProcessId={{row.PrintProcessId}}&printXMLId={{row.PrintXMLId}}">View&nbsp;<i class="icon icon-lg icon-file-o"></i> </a>

However, I still need to address the issue with the Web API call. When an error occurs, I receive a 400 error Ajax document. I hope to handle this differently and may need to inquire about it separately.

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

Using FabricJS ClipTo to Set Image or SVG as Canvas Border

Is there a way to apply the clipTo function to an image or SVG in order to constrain objects within the shape or outline? I'm looking to achieve a similar goal as the user in this post, but the solutions provided were not clear to me. I have success ...

Minifying HTML, CSS, and JS files

Are there any tools or suites that can minify HTML, JavaScript, and CSS all at once? Ideally, these tools should be able to: Identify links from the HTML document and minify the associated JavaScript and CSS. Remove any unused JavaScript functions and CS ...

getting v-model value updated when clicking button

How can I update the value of v-model DataJournals.Description in a looping data when a button is clicked? This is what I have attempted: template <tr v-for="(DataJournals, index) in DataJournal" :key="DataJournals.value"> <td> & ...

Unable to retrieve HTTP call response during debugging, although it is visible in the browser

When I send an HTTP request to create a record, I am able to see the added record id in the Network section of browsers like Chrome and Firefox. However, when I try to debug the code and retrieve the same id value, I encounter difficulties. I have tried us ...

Assorted Three.js particles

As a beginner in the world of three.js, I'm currently tackling the challenge of incorporating 1000 particles, each unique in size and color. The current roadblock I'm facing is that all particles end up the same color and size when using a Partic ...

Instruction to pay attention to the event

After noticing that I've added similar event listening code to many of my controllers, I began to think about how to streamline it. The code looks something like this: document.addEventListener("resume", function(e){ $scope.doSomething(); }, ...

Sophisticated tree ext-table drag-and-drop functionality (5)

I have been experimenting with using fancytree alongside the ext-table and dnd5 extension. Up to this point, everything has been working smoothly for me. My goal now is to be able to sort the table columns via dnd. Unfortunately, registering the content o ...

Updating nested forms in Angular 4

The nested form structure I am working with is a 'triple level' setup: FormGroup->ArrayOfFormGroups->FormGroup At the top level (myForm): this.fb.group({ name: '', description: '', q ...

Close CSS Accordion when Clicked

I have a CSS accordion that I would like to close each panel upon clicking. Can this be accomplished using only CSS, or will JavaScript need to be added? #accordion input:not(:checked) + div { display: none; } #accordion input:checked + div { displ ...

Illustrating Angular Scope and Controller Relationships in Sequence Diagrams

Seeking advice on creating sequence diagrams that involve Angular scopes or both Angular scopes and directives. Wondering if it's best to follow a flow like: User -> View -> $scope -> Controller -> Service Including $scope in the diagram ...

jQuery fails to locate class following AJAX reply

In my application, there is a cart feature where users can remove items from their cart, triggering a refresh of the contents using AJAX. However, I noticed that after removing an item from the cart, the response switches from asynchronous to synchronous m ...

Directive that accesses an element and adds an event listener

I'm in need of developing an attribute directive using Angular 1.5.5 that will restrict inputs based on the keypress event of a text box. The desired output should be like this: <input type="number" name="age" id="age" restrict-char="['e&apos ...

AWS Lambda applies quotes to create the event input

I'm encountering a problem with my Python 3.8 AWS Lambda function that is meant to handle form inputs from a web application. The data from the form inputs gets passed to the Lambda function and ends up in the event dictionary. However, lambda seems t ...

Achieving the minimum width of a table column in Material-UI

Currently I am in the process of developing a React website with Material-UI. One query that has come up is whether it's feasible to adjust the column width of a table to perfectly fit the data, along with some extra padding on both ends? Outlined be ...

AngularJS directive ngShow not working properly

It seems like I'm encountering some scope issues that I can't seem to resolve. Here's a rundown of the problem: I'm trying to create a custom directive called "my-create-content" that will insert an "img" HTML template into the element ...

Most effective method for retrieving data from a database to display on a React user interface

I am currently working on a project where I need to show the value of a collection property stored using mongoose in my react app's client side whenever a user clicks a button. I am attempting to retrieve the value on click and then use setState to s ...

Storing an inner object within a map using Javascript

// Here we have a script that aims to store 3D mesh objects in a map // The problem arises when attempting to access the stored object later this.meshMap = new Object(); // Function to add a mesh to the map using a specified key and URL t ...

Object selection feature in AngularJS BootstrapUI Typeahead

Currently, I'm working on integrating a typeahead feature in Angular using the URL http://angular-ui.github.io/bootstrap/. My goal is to have the typeahead field show complete addresses, but when a user clicks on an address, another field should be po ...

The performance of dom-repeat may be impacted when dealing with sizable objects, such as those containing base64 encoded images

Currently, I am encountering an issue while generating a dom-repeat using a list of objects. Each object in the list has an imgUrl key containing a large base64 encoded image. However, when I generate the dom-repeat in this manner, each item appears undef ...

Using THREE.js: Finding the nearest point between two rays in a THREE.js environment

In my current project with THREE.js, I am working with two THREE.Ray objects. Both rays have an origin point represented by a Vector3 and a direction indicated by another Vector3. I am currently exploring how to determine the nearest intersection point be ...