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

Tips for incorporating JavaScript into elements that have been modified using jQuery's .html() method

Consider this example: $('#key').on('click', function(){ $('.task').html("<button id='key'>Button</button>"+Date()); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.j ...

The barcode is not displaying when using javascript:window.print() to print

I am currently developing a Mean Stack App where I have a requirement to display a barcode. To achieve this, I am utilizing an AngularJS directive for generating a 128 barcode, and it is being generated successfully. However, when I attempt to print by cli ...

The Angular Google Maps Directive zooms in too much after the "place_changed" event has fired

Currently, I am developing a store locator app for DHL accessible at storefinder.hashfff.com/app/index.html For this project, I decided to utilize the angular-google-maps library for its features. However, in hindsight, working directly with the Google Ma ...

Which is the preferable option for creating a circular daily planner: canvas or SVG?

As a novice programmer delving into the world of coding (or at least hoping to), I have a question regarding graphic tools in html5 for my latest project. My goal is to create a planner app using electron, with the unique twist that the planner form sho ...

Is it possible to submit two forms simultaneously using jQuery or AJAX?

My plan is to save 2 forms, with the first form providing the Foreign key for the second form. This is my attempt at saving this using JavaScript: $("#btnSave").click(function (e) { e.preventDefault(); $('#workForm').submit(); ...

The Unpredictable Behavior of Angular Routing

In the Angular 1 Application, I have set up the following route: .state('myConfiguration', { parent: '/', url: 'myConfiguration', resolve: { ready: function() { // eslint-disable-next-line no-undef retur ...

What are the steps to modify the relative URL when using Express proxy?

To prevent the common CORS issue with client-side JavaScript code, I utilize a Node.js Express server. The server is configured with the following code: var app = express(); app.all('/Api/*', function (req, res) { proxy.web(req, res, { ...

Is it possible for me to recover the original password from a hashed password?

I am currently working on a hybrid mobile application that uses Node.js as the backend to store data in MongoDB. The server is set up correctly and I have defined routes to handle user requests. For password security, I am utilizing bcrypt to store hashed ...

When registering the onHardwareBackButton event in Ionic, the back button continues to successfully navigate to the previous page

I recently started working with Ionic and encountered an issue with the onHardwareBackButton event. The event is functioning properly and directing me to the register function, but even after going to the register function, it still navigates back to the p ...

What steps can be taken to utilize ajax for pagination and retrieving data efficiently?

My goal is to retrieve 3 specific data points from a webpage using the ajax get method. Here is the javascript code: <script type="text/javascript"> var busy = false; var limit = 15; var offset = 0; var id = 150; function displayRecords( ...

What's preventing me from using just one comparison condition in TypeScript?

The issue at hand is quite simple: An error occurred because I tried to compare a number with a 'Ref<number>' object. It seems ridiculous that I can't compare two numbers, but as I am new to Typescript, I would greatly appreciate some ...

Save the data from the JSON file when it is loaded

Currently, my factory is set up to load a JSON file. angular.module("app").factory("RolesFactory", ['$http', '$q', function($http, $q) { var d = $q.defer(); $http.get('events.json').success(function(data) { d. ...

Discover the #ID and apply AddClass through the URL in the Navigation Jquery

I am currently in the process of developing a website and I am looking to navigate from one link to another using IDs. Here is an example of what I am trying to achieve: Page Name: index.html <a href= "collection.html#rings">Rings</a> Page N ...

Dynamic styles object for React components with inline styles

I have a styles object let styles = { step_div:{ height:'150px', } } I'm trying to display multiple div elements with different colors using React class Layout extends React.Component{ constructor(props) { super(props); ...

How can I retrieve a password entered in a Material UI Textfield?

My code is functioning properly, but I would like to enhance it by adding an option for users to view the password they are typing. Is there a way to implement this feature? const [email, setEmail] = useState(''); const [password, setPassword] = ...

Struggling to locate a straightforward sidebar solution without relying on Bootstrap. Finding an easy-to-use answer seems

I have a lightweight and easy-to-understand code for a website project with two panels. The first panel on the left is a large navigation (270px width, top to bottom, similar to a wiki) with approximately 30 unordered list items. Next to it is the second ...

Send information to modal using Javascript

I am facing an issue when trying to transfer data from JavaScript to a Bootstrap modal. Here is how my code looks: Modal: <div id="myModal" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> ...

Express Power Tool - Error: app.set function is undefined

My journey with creating an API using Node/Express began with a solo endeavor to learn the basics in a 'naive' manner. The initial setup worked smoothly, prompting me to experiment with express-generator. Upon completion of the setup process, th ...

Merging a VUE project and a .NET framework project to unleash their full potential

Currently, I am working on a project that involves using VUE for the client side and .net framework for the server side. However, these two components are hosted as separate projects, requiring me to open different ports during development. I am aware tha ...

My Node/Express service seems to be generating a span in the logs, but I am unable to locate my service in the Ja

Currently, I am in the process of setting up Jaeger tracing for my Node.js microservice using Express.js. After adding a basic GET request handler in my Express app and hitting the endpoint via curl, I notice that a span is created in the logs. However, w ...