The expression for AngularJS ng-switch-when

I am currently working on creating a tabbed menu using the ng-switch directive.

Within my Ctrl (streams), I have set the tabs and am keeping track of the selected one as selection:

app.controller("StreamCtrl", function($scope) {
$scope.streams = [{
    title: 'latest',
    icon: 'time',
    data: "Hi, I'm data."
}, {
    title: 'popular',
    icon: 'fire',
    data: "Hi, I'm data too!"
}];

$scope.selection = $scope.streams[0];

$scope.getCurrentStreamIndex = function(){
    // Get the index of the current stream given selection
    return $scope.streams.indexOf($scope.selection);
};

// Go to a defined stream index
$scope.goToStream = function(index) {
    if($scope.streams[index]) {
        $scope.selection = $scope.streams[index];
    }
};
});

In my view (index.html), I am using ng-repeat to create a container for each tab:

<section class="streams" ng-controller="StreamCtrl" ng-switch="stream.title == selection.title">
        <section class="stream" ng-switch-when="true" ng-repeat="stream in streams">
            {{stream.title}}
            <div class="loaderContainer"><div class="loader"></div></div>
        </section>
    </section>

The issue I am facing is with the ng-switch-when statement, as it does not accept an expression.

If I could use

ng-switch-when="{{stream.title}}"
then I believe I could utilize ng-switch="selection.title" and resolve the issue.

How can I structure an ng-switch expression to match a dynamically generated list?

Answer №1

Alright, take a look at this for some guidance to help you along:

http://jsbin.com/okukey/1/edit

Fresh html code:

  <div ng-controller="StreamCtrl">
  <section class="streams"  ng-repeat="stream in streams">
        <section class="stream">
            {{stream.title}}
            <div class="loaderContainer" ng-show="stream == selection"><div class="loader">SELECTED</div>
        </section>
    </section>
  </div>

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 it possible to verify the versions of node and npm prior to running an npm install command?

To ensure only specific versions of node and npm are used before a user can run the npm install command on my module, I need to set certain criteria. According to NPM documentation, I can use the engine attribute for this purpose: "engines": { "nod ...

Discover how to efficiently load and display a JSON array or object using JavaScript

I am new to learning about json and d3, having just started a few hours ago. While I have basic knowledge of javascript, I need help with loading a json file and displaying all the arrays and objects on the console using d3. I tried to do it myself but unf ...

Switch tabs with JavaScript

I'm encountering an issue with changing tabs using JavaScript and Ajax. The script I have works when not using Ajax but fails to work properly with it. Here is the script: $(document).ready(function(){ $("#mtabs li").click(function() { ...

What could be the reason behind encountering an NaN error while using these particular functions?

I recently delved into the world of JavaScript, starting my learning journey about two months ago. While going through a few tutorials, I stumbled upon an intriguing project idea. However, I've hit a roadblock that's impeding my progress. Every t ...

Is it possible for :hover to function with td elements in jQuery?

I created an HTML Table that includes a hidden infobox within one of the td elements. <style type="text/css"> .infobox{ display: none; background-color: #FFDB8F; font-size: 11px; } td { border: 1px solid; ...

Using React to apply various filters to an array

Just getting started with react and working with an array of objects named arrayToFilter that I need to filter in multiple ways. Whenever a user changes the filter options, all filters should be applied to the array and the results should be stored in a fi ...

Sending PDF file to client's request using PDFKIT and Strapi (Koa) via HTTP response

My goal is to send a PDF file as a response to a GET request on my Strapi endpoint. The current Strapi controller, which uses Koa, is structured like this: const PDFDocument = require("pdfkit"); module.exports = { async printOne(ctx) { const doc = ...

The function document.getElementById is unable to select multiple elements simultaneously

I've been tackling a loading issue. I have a visible div, #loading, and multiple hidden divs, #message. I also have a JavaScript function. function loading() { setTimeout(function() { document.getElementById("loading").style.display = " ...

Using a variable called "Url" instead of "window.location" in JavaScript is not functioning as expected

onclick="window.location = 'index.php'" While the above code seems to be functioning correctly, the code below is not performing as expected. Can you identify the issue? <?php $link = 'index.php'; ?> <script> var link= ...

Error: The 'price' property is undefined and cannot be read at path C:NODEJS-COMPLETE-GUIDEcontrollersshop.js on line 45, character 37

I have been attempting to add my products to the cart and calculate the totalPrice, but I keep encountering an error. Here is the error message:- enter image description here. Below is the code from my shop.js file:- exports.postCart = (req, res, next) =&g ...

Cannot seem to get my AJAX code working correctly to show comments

Having some trouble with my comment system code. The PHP part is working fine, comments are being inserted into the table without any issues. However, I am struggling with the Ajax part. The comments are not being displayed unless the page is reloaded. C ...

What steps should be followed to create an npm script that can execute multiple commands for running test cases?

When running the end-to-end (e2e) tests for my AngularJS application, I find myself having to execute a series of commands in separate shell sessions: // start the selenium server webdriver-manager start // initialize a http server to host the current fi ...

When I pass a date from Angular to C#, I receive a default datetime value

When sending a date through Angular and C#, I am encountering an issue where I receive an unintended default date from datetime. For instance, if I send 05/06/2022 in C#, I end up receiving {01/01/0001 00:00:00}. Is there a way for me to retrieve the orig ...

Issue encountered while managing login error messages: http://localhost:3000/auth/login net::ERR_ABORTED 405 (Method Not Allowed)

I am working on the /app/auth/login/route.ts file. import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs' import { cookies } from 'next/headers' import { NextResponse } from 'next/server' export async functi ...

Transmit information from a bean to JavaScript using JSON in JavaServer Faces

I need help transferring my arraylist from a managedBean to JavaScript code. The bean code is shown below: public void getDataAsJson(){ String [] dizi={"Tokyo","Jakarta","New York","Seoul", "Manila","Mumbai","Sao Paulo","Mexico City", ...

Sort through a collection of objects depending on various criteria pulled from a separate array of objects

I am working with an array of objects called c, which looks like this: c = [ { name: 'abc', category: 'cat1', profitc: 'profit1', costc: 'cost1' }, { name: 'xyz', catego ...

The Kendo Grid is refusing to show up within the popup window

I am new to using Angular 2 and Kendo UI. Currently, I am attempting to include a grid inside my pop-up window. While I have successfully displayed the pop-up, adding the grid has proven challenging. The grid is not appearing as expected ...

Executing an HTTP request with JavaScript to interact with Salesforce

Looking to connect Salesforce with Recosence, an external system. The scenario involves Recosense pushing data to Salesforce for storage. I have successfully created a post HTTP service and tested it in Postman, which generates an access token and records ...

What is the reason that the selected/checked attributes of child select and input elements are not preserved when transferring HTML from one element to another?

In the world of jQuery, there lies a curious phenomenon. When you dare to set the HTML of one element as the HTML of another element, a strange omission occurs. The checked attribute of checkbox inputs and the selected attribute of select inputs are myster ...

The button fails to appear when navigating

Hello everyone, I have a code snippet for a functional back button in my Angular application, but I'm facing an issue with adding it to an existing constructor. I already have a constructor for navigating between pages, but it seems the back button co ...