Display the active item in ng-repeat using conditional ui-view

Here is the situation:

I'm working with a list of items that are displayed using ng-repeat.

Each item has its own template.

When a user clicks on an item, that particular item is marked as "active" and gets a different template compared to the rest of the items in the list.

By default, the first item in the list is always set as "active".


My question is: Can I achieve this functionality using ui-router? I'm aware that I can use a templateURL function to access the state parameters, but it would affect all items in the list.

If possible, I'd prefer not to use ng-if/ng-switch as the active item may have multiple nested states with unique templates.

angular.module("app", ["ui.router"]);

angular.module("app")
  .config(function($stateProvider) {
    
    $stateProvider
      .state("list", {
        url: "/list",
        abstract: true,
        templateUrl: "list.html"
        controller: "ListCtrl",
        controllerAs: "listC",
      })
      // How can I configure this to change the template for the "active" item in the list?
      .state("list.item", {
        url: "/list/item/:itemId"
      });
  });

angular.module("app")
  .controller("ListCtrl", function($state) {
      // This data would be fetched asynchronously
      this.items = [
        {id: 1, name: "One"},
        {id: 2, name: "Two"},
        {id: 3, name: "Three"},
      ];
      
      $state.go("list.item", {itemId: this.items[0].id})
  });
<div ng-app="app" ui-view></div>

<script type="text/ng-template" id="list.html">
    <ul>
      <li ng-repeat="item in listC.items" ui-view></li>
    </ul>
</script>

<script type="text/ng-template" id="list-item.html">
    <a ui-sref="list.item({itemId: item.id})">{{ item.name }}</a>
</script>

<script type="text/ng-template" id="list-item-active.html">
    <h3>{{ item.name }}<h3>
</script>

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

Streamlining the login process in AngularJS to eliminate the need for repeated logins

I have set up authentication and authorization using AngularJS, Jersey REST, and Spring Security. Once a user logs in, they can call the "create" method below to save their information: .factory('Session', function () { this.create = functio ...

Hidden Document Scroll Offset

When CSS is used to hide scrollbar html, body { width: 100%; overflow-x: hidden } The above code snippet removes the scroll from the window but triggers it on the body element. To calculate the scroll values in such cases, you can use: pageOffset = ...

Rect cannot be resized using mouse events

I am currently working on resizing the rectangle inside the SVG using mouse events. To achieve this, I have created another circle shape at the right bottom edge of the rectangle and implemented resize events on that shape. However, I'm facing an issu ...

Prevent Scrolling of Body Content within a Specified Div

In my current situation, I am dealing with a large number of divs (over 300) that are being used as part of an interactive background. The issue arises when viewing the page on mobile versus desktop – there are too many divs on desktop, causing excessive ...

Leverage closures within Underscore.js templates for enhanced functionality

Is there any benefit to utilizing a closure in an underscore template for purposes such as keeping track of counters? Here's a simple example: <% (function( models ){ var length = models.length-1, section = ""; _.each( models, functi ...

How can we use the useState hook in React to dynamically generate state variables?

I'm currently working on a React app where input fields need to be stored in the state. While I can use the useState hook to easily store these values, the challenge I'm facing is that I don't know what fields are needed since they are retri ...

Using various hues for segmented lines on ChartJS

I am working with a time line chart type and I want to assign colors to each step between two dots based on the values in my dataset object. In my dataset data array, I have added a third item that will determine the color (if < 30 ==> green / >30 ==> red ...

Tips for binding to a single input box within an ngFor loop

Can anyone lend a hand with some code? I'm working on a straightforward table using ngFor, but I'm facing an issue with input binding. The problem is that all the input fields generated by ngFor display the same value when typing. How can I preve ...

Always ensure that only one div is visible at a time

I am currently working on a project where I cannot use ng-cloak due to the way the css files are loaded. I have been exploring alternative solutions and have tried a few different approaches. My main goal is to ensure that two icons are never shown at the ...

The use of "app.use("*") appears to be triggering the function repeatedly

app.use("*", topUsers) is being invoked multiple times. The function topUsers is called repeatedly. function topUsers(req, res, next){ console.log("req.url", req.url) findMostUsefullReviewsAggregate(6) .then(function(aggregationResult){ ...

Steps for generating random numbers from a set of given numbers

I am faced with a scenario where I need to generate random numbers based on a given set of numbers. For instance, if I have an array num=[23,56,12,22], I would like to obtain a random number from this array. ...

Ways to verify the presence of an element in a list

I found this interesting JS code snippet: ;(function ($) { $('.filter-opts .opt').click(function(){ var selectedName = $(this).html(); $('.append').append('<li>' + selectedName + '</li> ...

AngularJS: Error message stating that '$scope is undefined'

Encountering '$scope is not defined' console errors in this AngularJS controller code: angular.module('articles').controller('ArticlesController', ['$scope', '$routeParams', '$location', 'Au ...

The AngularJS dependency injection system allows developers to easily manage dependencies using arrays and the $inject

Being new to angularJS, I'm seeking some insight on dependency injection. After some research, here's what I've gathered: I have 2 service files (using factories): -mogService.js angular.module('anglober.services').factory(&apo ...

Utilizing React JS with Material-UI Autocomplete allows for seamlessly transferring the selected item from the renderInput to the InputProps of the textfield component

Exploring the functionality of the updated version of Autocomplete, my goal is to ensure that when an element is chosen from the dropdown menu, the input format of the text field will be modified to accommodate adding a chip with the selected text. Is the ...

Unit Testing Angular controllers with an external variable in Jasmine framework

Looking for ways to create a unit test for a controller that utilizes a $scope.action variable that is defined outside the controller. controller( "MyController", [ "$scope", "$window", "httpInterceptor", function($scope, Service1, Service2, $wind ...

Can you explain how to utilize prop values for setting attributes in styled-components v4 with TypeScript?

Overview Situation: const Link = styled.a` border: solid 1px black; border-radius: 5px; padding: 5px; margin: 10px 5px; `; type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement>; const LinkAsButton = styled(Link).attrs<ButtonP ...

Guide for integrating the shadcn/ui Range Date Picker within a Form

Encountering an issue with using The Range Date Picker within the Form component. Specifically, I am looking to store {from, to} values of the range in an object, however, utilizing an object as a Form field value results in error messages not functioning ...

Packages starting with @ are found in the Node.js ecosystem

What's the deal with libraries in Node.js starting with @ symbols? Is there a specific convention for this? I feel like I'm missing something obvious here. ...

Angular with Firebase: How to ignore a field in a query

I am curious to see if my current structure is compatible with Firebase, or if I need to make adjustments. Let's take a look at an example using the "/rooms" endpoint, which contains an array of Room objects: export class Room { id: number; p ...