Updating information in the view when tabs are clicked in AngularJS

As a beginner in Angular, I'm seeking some guidance!

I have a webpage with three tabs (using Angular Material for tab functionality) that display different sets of records. Each tab should show records based on their status (e.g., OPEN tab displays 'Open' status records, CLOSED tab shows 'Closed' status records, etc.):

<div ng-controller="SomeController as someCtrl">
<md-content>
    <md-tabs>
        <md-tab id="tab1" >
            <md-tab-label>OPEN</md-tab-label>
            <md-tab-body>                
                <div ng-view></div>
            </md-tab-body>
        </md-tab>
        <md-tab id="tab2" md-on-select="someCtrl.showTaskListing('Error')">
            <md-tab-label>EXCEPTIONS</md-tab-label>
            <md-tab-body>
                <div ng-view></div>
            </md-tab-body>
        </md-tab>
        <md-tab id="tab3" md-on-select="someCtrl.showTaskListing('Closed')">
            <md-tab-label>CLOSED</md-tab-label>
            <md-tab-body>
                <div ng-view></div>
            </md-tab-body>
        </md-tab>
    </md-tabs>
</md-content>
</div>

Below is the controller code (RequestSenderService acts as a utility service for $http operations):

angular
    .module('SomeModule')
    .controller('SomeController', someController);

function SomeController($scope, $location, $http, RequestSenderService) {

    showTaskListing('Incomplete');

    function showTaskListing(status) {

        var httpResponsePromise = RequestSenderService.sendRequest("/getTasks?status=" + status);

        httpResponsePromise.then(
            function (response) {
                setTimeout(function () {
                    $scope.$apply(function () {
                            $scope.tasks = response.data;
                            for (task in this.tasks) {
                                task.isSelected = true;
                            }
                        }
                    )
                }, 1000);
            }
        )
    }

Routing configuration:

angular.module("SomeModule",["ngRoute","ngMaterial"])
    .config(function($routeProvider){
        $routeProvider
            .when("/tasks", {
                templateUrl : "js/tasklist/tasklist.partial.html",
                controller : "SomeController as someCtrl"
            })
            .when("/addTask", {
                templateUrl : "js/taskDetails.partial.html",
                controller : "AnotherController as anotherCtrl"
            })
            .otherwise( {
                redirectTo : "/tasks"
            })

    }) ;

Template:

<div>
    <table style="width:100%">
        <tr>                
            <th>Task Name</th>
            <th>Description</th>
            <th>Type</th>
        </tr>

        <tr ng-repeat="task in tasks">                
            <td ng-bind="task.title"></td>
            <td ng-bind="task.description"></td>
            <td ng-bind="task.type"></td>
        </tr>
    </table>
</div>

When loading the page (/tasks), data loads correctly on the first tab, but all tabs display the same data without refreshing when switching between them using md-on-select. Any help would be appreciated. Thanks.

Answer №1

Greetings! I recently came across an Angular material tab demo and made some tweaks to suit your requirements. Feel free to check out the link

All you have to do is make some adjustments.

$scope.selectedTab = function(index) {
    // your httpResponsePromise
}

It should work fine, just disregard the CSS part as it was simply copied from the angular material demo. Make sure to set up your HTML and JS in a similar manner to mine.

In essence, md-on-select is an expression that is evaluated when clicked and not an event handler. Thus, you need to save

md-on-select="result = selectedTab()"
and then place {{result}} in your template.

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

Calculating "Time passed" utilizing a predefined timestamp. ("2012-03-27 16:01:48 CEST")

Is there a way to automatically convert the timestamp "2012-03-27 16:01:48 CEST" into "1 hour and 22 minutes since" with time zone management? I'm unable to modify the original timestamp format, which will remain as "2012-03-27 16:01:48 CEST" or simil ...

Exchanging information rows between two tables within an Angular application utilizing Angular Material

Recently, I created a sample angular application that includes a popup component. Within this popup component, I have incorporated two tables. The first table displays a list of data rows, while the second table is currently empty. One of the key feature ...

Swap out a component for another using a higher order component (HOC perhaps?)

I have noticed that certain libraries like "framer-motion" in react utilize this syntax, for instance to include an animated H1: <motion.h1> where the h1 tag is enhanced with animations specified in the component's props. What confuses me is ho ...

Generate an array of responses within a child component and then send this array to the parent component using Vue.js 2

Currently, I am in the process of creating a survey using Vue 2. My goal is to construct and emit the answers array to a parent component named Evaluation. You can view the structure of this array here: https://pastebin.com/rLEUh56a Within my project, the ...

Angular is experiencing issues with properly interpreting API responses from a PHP server

I have been requesting data from my API, but it seems like the data is not displaying correctly. I've checked my code multiple times, but I can't seem to find where the issue lies. Here is the code snippet: The JavaScript portion looks like: f ...

Refresh the webpage upon clicking the link in PHP

I'm looking to refresh my page when a user clicks on a link. I tried using the window.location.reload(true) function, but it only reloaded the page with the cached version. I need it to completely reload without using cache. I am testing this on Googl ...

how to retrieve date value from firebase and format it with moment.js

My current setup involves using Firebase as the database and Moment.js for managing dates in my Ionic Project. I store the necessary dates in Firebase but I need to calculate the difference between two specific dates in terms of hours, days, etc. However ...

What is the correct method for notifying the progress of time using jQuery?

I'm looking for a way to display the processing time of my PHP code using jQuery Here's an example of my PHP code : <?php //some queries to database ?> Below is my jQuery code: $.ajax({ type: "POST", url: "action. ...

A guide to looping through a JSON array

After utilizing the Gson library to generate a JSON from Java, the output I receive is as follows: [{"title":"title1","author":"author1"},{"title":"title2","author":"author2"}] What would be the most effective approach for parsing and accessing these val ...

Encoding multiple arrays in JSON using Zend PHP

I'm encountering an issue with the JSON functionality in Zend and JavaScript. Initially, I attempted to encode a single array that contained a certain number of models in the following manner: echo json_encode(Application_Model_Marker::getMarkers()) ...

Bus Boy's employment record shows no termination

I am working on a Next Js project that includes an upload image endpoint. const busboy = require('busboy') export default function handle(req, res) { const bb = busboy({headers: req.headers}); bb.on('file', (fieldname, file, fi ...

jQuery function duplicates row in table

I am trying to utilize jQuery to generate a table, but I seem to be encountering an issue with my code: function showGrid(url, container, columns, page) { jQuery(container).empty(); var tr = jQuery("<tr class=\"mobileGridHeader\"> ...

Navigating with AngularJS and jQuery Mobile

Currently I am developing a web application utilizing jQuery and jQuery-Mobile, however there has been a request for me to incorporate AngularJS into the project. The issue I am facing primarily involves handling routing within the application, despite res ...

Issue with passing reactive property to component in Vue 3 application

I am currently working on a Vue 3 application and I am in the process of setting up a store for state management. Within this application, I have several important files that play different roles: app.vue component.vue main.js store.js These files contai ...

When there is no error, the catch block in Javascript promises is still being

I recently developed a function that calls an API as shown below. The goal was to display the message from setMessage on the frontend. However, I encountered a strange issue where the .catch block message briefly flashes in setMessage() even when there is ...

Monitor a function triggered by a click event

Why is my spy test always returning false? I'm attempting to spy on a function that is called during the click event of an element, but it's not working as expected. spec: describe('button', function() { before(function() { this ...

I'm unable to modify the text within my child component - what's the reason behind this limitation?

I created a Single File Component to display something, here is the code <template> <el-link type="primary" @click="test()" >{{this.contentShow}}</el-link> </template> <script lang="ts"> imp ...

Discover the method of using jQuery to identify a modification on a select element with an ID that is generated dynamically, and modify the choices available for selection

Our team has successfully implemented a user "profile" page with a two-column HTML table using a web framework (Rails 4.2.x). Users can dynamically add rows to this table by clicking an "add row" button, and everything is functioning as expected. The two c ...

Invoke a function using the output of a different function

There is a function whose name is stored in the value of another function, and I need to invoke this function using the other one. The function I need to call is popup() random() = 'popup()' if ($.cookie('optin-page')) { } I attemp ...

How can we incorporate Django template tags into our jQuery/JavaScript code?

Is it possible to incorporate Django's template tags within JavaScript code? For example, utilizing {% form.as_p %} in jQuery to dynamically inject forms onto the webpage. ...