Updating Angular UI routes with various parameters

When utilizing Angular UI router, I have a route configured in the following manner

$stateProvider.state('edit', {
    url: '/:file/:page',
    ...
}

After modifying the route from /edit/file1/page1 to /edit/file1/page2, the view does not refresh. Despite calling $state.go to redraw the page, changes like the back button do not reflect the updated route.

Could someone provide guidance on how to update the route for the view to be refreshed?

Appreciate it.

Answer №1

It seems like your goal is to dynamically set the templateUrl based on the values of :file and :page in the $stateParams.

Although you haven't provided details on how you are using $state.go, I have created an example that demonstrates different ways to switch between states with or without { reload: true }.

angular.module('exampleApp', ['ui.router'])
  .config(function($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/file1/page1');
    $stateProvider
      .state('edit', {
        url: '/:file/:page',
        controller: 'FilePageCtrl',
        templateUrl: function($stateParams) {
          return $stateParams.file + "-" + $stateParams.page + ".html";
        }
      });
  })
  .controller('FilePageCtrl', function($scope, $state) {});

Below are some sample links that trigger state changes but do not re-execute the controller and template if they are already in the current state:

<a ui-sref="edit({ file: 'file1', page: 'page2' })">/file1/page2</a>

This can also be achieved inline with ng-click or within a controller:

<a href ng-click="$state.go('edit', { file: 'file1', page: 'page2' })">/file1/page2</a>

The following two examples will force the controller and templateUrl to execute, regardless of whether the current state matches:

<a ui-sref="edit({ file: 'file1', page: 'page2' })" ui-sref-opts="{ reload: true }">/file1/page2</a>
<a href ng-click="$state.go('edit', { file: 'file1', page: 'page2' }, { reload: true })">/file1/page2</a>

To learn more about using dynamic templateUrl, you can visit this link: https://github.com/angular-ui/ui-router/wiki#templates

For a live demo, check out this working example: http://plnkr.co/edit/JUgUB3I675Kh7kKkitgJ?p=preview

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

Update the content of the document element by assigning it a lengthy string

I'm utilizing JQuery to dynamically assign content to a div element. The content has numerous lines with specific spacing, so this is the approach I am taking: document.getElementById('body').innerHTML = "Front-End Developer: A <br/> ...

Leveraging the power of ES6 syntax in Node scripts with Babel

My npm CLI tool utilizes ES6 syntax from BabelJS, specifically arrow functions. Within the entry point of my tool, I'm using the following require: require('babel-core/register'); var program = require('./modules/program.js'); I ...

What is an alternative way to show the contents of a JSON file without directly accessing it

Recently, I stumbled upon an amazing website - where I have been exploring to learn something new. The website prominently features the use of Ajax and some fascinating javascript without any additional libraries. Within a single javascript file on this ...

Show or hide table rows in AngularJS based on various conditions or hide all rows

I am looking to customize my table by only displaying rows that match specific variables and have the ability to hide/show all rows. How can I implement a hide/show all functionality while keeping the <"a> tags, with the option to add more variables ...

Creating variables in Typescript

I'm puzzled by the variable declaration within an Angular component and I'd like to understand why we declare it in the following way: export class AppComponent { serverElements = []; newServerName = ''; newServerContent = &apos ...

Whenever I relocate the button's position within the Bootstrap framework, it seems to end up in a completely different

body <div class="container" style="margin-top: 70px;"> <div class="formlogin bg-light bg-gradient shadow-lg p-3 mb-5 bg-body rounded col-8"> <p id="signText"> Signin Fo ...

Manipulate an object in Three.js using the ObjLoader functionality

I'm currently working on manipulating an object loaded using OBJLoader in Three.js. The issue I'm facing is that while it's easy to manipulate the object once, I can't figure out how to do so during the animate loop or anywhere outside ...

Changing the value of a JavaScript variable within the .Net Codebehind

I've run into an issue where I need to update a JavaScript variable after post-back. My initial approach was to use the ClientScript.RegisterStartupScript function, which worked fine during the first page load but failed on subsequent postbacks. I inc ...

The SEMrush API is not displaying an 'Access-Control-Allow-Origin' header on the requested resource

When attempting to utilize the SEMrush API, I made a request using jQuery as shown below: $(document).ready(function() { $.get( 'https://api.semrush.com', { type: 'phrase_this', key: ' ...

Is it possible to remove or delete a module in AngularJS?

Is there a way to clear the memory of a previous module in my app that I won't be using after routing to a different location? For instance, let's say my main Angular module is "WebApp" which has dependencies on modules like "catalogApp", "Payme ...

SignOut operation in Express.js Firebase instantly responds with a status of 200 to the client-side Fetch POST request

I'm currently facing an issue with my POST request setup using the Fetch API in my client-side JavaScript. The request is being sent to my server-side JavaScript code which utilizes Express Js and Firebase Auth. The problem arises when the auth().sign ...

Can minification of JS be achieved in a Jekyll environment?

Currently, I am in the process of developing a project with Jekyll and one of the requirements is to minify HTML, CSS, and JS. I was wondering if Jekyll has built-in features for JS minification. It may seem like a simple question, but since I am new to ...

What could be the reason for the cardDetails component, which is supposed to display information received via a Vuex action, not appearing

Recently, I've been diving into Vue and Vuex while working on a small app that showcases events. Users can click on event cards to view more details using the card's ID. I've moved all the code to the Vuex Store, but I'm encountering is ...

Using the let keyword from another component within the main React application: Helpful tips and tricks

I'm new to React and I want to be able to use the formIsValid state from one component in my main App.js file. When formIsValid is false, I want the DeliveryNote component to be visible, but when it changes to true, I want to hide the DeliveryNote com ...

Adjust the size of a collapsed element in a bootstrap accordion panel

I am working with a classic bootstrap accordion in my code. I want to adjust the width of the expanded ul/li elements when a h5 element is clicked (decrease width). Currently, the width expands fully when the h5 element is clicked. Can anyone provide ass ...

Applying inline styles in AngularJS

I am currently experiencing a challenge when attempting to apply a CSS property to an element after its children have been rendered. Specifically, I am trying to set the height of the parent element to match the height of its first child. However, the chil ...

Utilizing Angular's globally accessible variables

As we make the switch to Angular.js for our webapp, we are faced with the challenge of storing global data. In the past, we used a global object called app to store various functions and variables that needed to be accessed across different parts of the ap ...

Tips for utilizing the "Sign In with Apple" feature through Apple JS

For implementing "Sign In with Apple" on a web platform using Apple JS, you can refer to the code example available at this link. Now, the query arises: where can I find the Client ID required for this process? I have tried using the app id identifier fro ...

What is causing the lack of updated data on the components when navigating to a different page? (Vue.JS 2)

I am working with 2 components The first component looks like this : http://pastebin.com/M8Q3au0B Due to the long code, I have used pastebin for it The first component calls the second component The second component is as follows: <template> ...

Retrieving video information using Dailymotion API with JSON and jQuery

I have been struggling to understand the issue even after consulting the Dailymotion API and various sources. I am attempting to retrieve data from Dailymotion for a specific video ID by using the following code: $.getJSON('https://api.dailymotion.co ...