Nested views in angular-ui-router offer the ability to have multiple

My application consists of 3 views (A, B, C) and 2 states (1, 2).

Here is the basic HTML structure:

<div ui-view="A"></div>
<div ui-view="B"></div>
<div ui-view="C"></div>

The two states are named list and create. In both states, the template and controller for views A and B remain the same, while view C should change its templates and controllers. However, when I manage to change the content of view C, it also refreshes views A and B causing their controllers to run again.

I am looking for the correct way to organize the router to prevent this issue.

Here is my current JavaScript code:

$urlRouterProvider.otherwise("/basestate/list");

$stateProvider
    .state('baseState', {
        url:"/basestate",
        templateUrl: "basestate.html",
        controller: 'BaseStateCtrl'
    })
    .state('baseState.list', {
        url: "/list",
        views: {
            "viewA@baseState": {
                templateUrl: "viewA.html",
                controller: "ViewACtrl"
            },
            "viewB@baseState": {
                templateUrl: "viewB.html",
                controller: "ViewBCtrl"
            },
            "viewC@baseState": {
                templateUrl: "list.html",
                controller: "listCtrl"
            }
        }
    })
    .state('baseState.create', {
        url: "/create",
        views: {
            "viewA@baseState": {
                templateUrl: "viewA.html",
                controller: "ViewACtrl"
            },
            "viewB@baseState": {
                templateUrl: "viewB.html",
                controller: "ViewBCtrl"
            },
            "viewC@baseState": {
                templateUrl: "create.html",
                controller: "createCtrl"
            }
        }
    });

Answer №1

In order to accomplish this, you will need to freeze your viewA and viewC at the level of baseState and declare that state as abstract:

.state('basestate', {
    url: '/basestate',
    abstract: true,
    views: {
      "viewA": {
        templateUrl: "viewA.html",
        controller: "ViewACtrl"
      },
      "viewB": {
        templateUrl: "viewB.html",
        controller: "ViewBCtrl"
      },
      "viewC": {
        template: '<div ui-view="viewC_child"></div>'
      }
    }
})

It's important to note that for viewC, we are creating a placeholder that will hold our nested views (either list or create):

.state('basestate.list',{
    url: "/list",
    views: {
        "viewC_child": {
            templateUrl: "list.html",
            controller: "ListCtrl"
        }
    }
})
.state('basestate.create', {
    url: "/create",
    views: {
        "viewC_child": {
            templateUrl: "create.html",
            controller: "CreateCtrl"
        }
    }
})

Take a look at this example on Plunker and watch out for commas in your code :)

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

Locate a specific element within a multi-dimensional array based on a partial match of one of its properties with a provided text

I am working with an array that includes three properties: ID : number Name : string Description :string ItemList :array<T>=[] and ItemListCopy :array<T>=[] Currently, this array is linked to the ng-multiselect dropdown During the onFilt ...

Strange actions observed in AJAX POST request

This code snippet functions perfectly when I set a breakpoint in the backend and allow the value to be zero before continuing with the rest of the code. However, if I don't set a breakpoint and let it run, the value will become 2 and cause a break in ...

AngularJS: Dealing with scope loss in a directive following a promise

I am currently facing a major issue within my Angular application. There is a Factory in my code that makes an HTTP request to retrieve spoolers and returns a promise. Spooler.prototype.load = function () { var self = this; var deferred = $q.defe ...

Struggling with Enum Field Implementation in Mongoose Schema

Issue with Mongoose Enum Field Validation const userSchema = new mongoose.Schema({ name: { type: String, required: [true, 'Please provide your name!'] }, email: { type: String, required: [true, 'Please enter your email ad ...

Enhance Your Website with Bootstrap 5 Slider/Carousel for Product Showcase

I am attempting to create a slider/carousel of products similar to the image shown below using Bootstrap 5: https://i.sstatic.net/7FhPw.png Here is the current code I have: <style> <!-- Temporary --> .carousel-control-next-icon { bac ...

Prop in a React component is undergoing mutation

I encountered a strange situation where a prop in a React component is being changed. Although it's technically not a mutation since it's an array in JavaScript, it should not be modified. To replicate the issue, I created a simple example: htt ...

Managing State with Vuex: Storing Getter Results in the State

I'm encountering an issue with my Vuex.Store: My goal is to retrieve an object (getter.getRecipe) by using two state entries as search criteria (state.array & state.selected) through a getter. Then, I want to store the outcome in my state (state. ...

unable to connect a value with the radio button

I am struggling to incorporate a radio group within a list of items. I am facing difficulty in setting the radio button as checked based on the value provided. Can anyone provide me with some guidance? Here is the HTML code snippet: <div *ngFor=" ...

Unable to handle web service response in error scenario

function CreateCinemas() { Services.AdminWebServices.CreateCinemasSVC(oCinemas, OnSuccessCinemas, OnError, OnTimeOut); } function OnSuccessCinemas(response1) { Services.AdminWebServices.AddTicketPricesSVC(oTicketPrices, OnSuccessTicketPrices, OnError, ...

The npm package for google-spreadsheet.js is experiencing issues and is not functioning correctly when trying to replicate the GitHub

After attempting to implement the basic example code provided at https://github.com/theoephraim/node-google-spreadsheet, I encountered an issue. For instance: const { GoogleSpreadsheet } = require('google-spreadsheet') const creds = require(&apo ...

Can PHP be used to create a new page whenever Javascript history.go(-1) is triggered?

A PHP file (a.php) is currently sending the following headers: <?php header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Pragma: no-cache'); ? ...

Scroll to the bottom of a textarea in React by automatically shifting focus

I am having issues with appending text to a textarea and then scrolling to the bottom in order to keep the latest content within view. However, it seems that this process is causing the browser to crash or run out of memory. Can someone provide assistance ...

Parsing Date and Timezone in AngularJS

I have the following line of code. <span> {{messages.created_time}}</span> Here is the output: 2017-04-29T12:46:49+0000 However, I want the output to display as follows: n minutes ago // if less than one hour according to user timezone 22 ...

Construction of jQuery Events

I have experience working with jquery tools and recently started exploring the twitter bootstrap src. One interesting thing I've observed is the utilization of the $.Event constructor for event triggering. For example, in cases like the bootstrap mo ...

The function type '(state: State, action: AuthActionsUnion) => State' cannot be assigned to the argument

I have encountered a persistent error in my main.module.ts. The code snippet triggering the error is as follows: @NgModule({ declarations: [ PressComponent, LegalComponent, InviteComponent ], providers: [ AuthService ], imports: ...

Strategies for detecting file import failures within Angular UI Grid

Currently implementing Angular UI Grid for file imports. Here is my configuration: enableGridMenu: true, importerDataAddCallback: function (grid, newObjects) { Encountering an error when trying to import non-CSV files: uncaught exception: UNEXPECT ...

How can I change an array from OData into JSON format?

I've been working on finding a solution to properly handle an OData response in JavaScript. The issue I am facing is that the response comes back formatted as an array rather than JSON, preventing me from using JSON.parse(mydata) with the current data ...

Enhance photo size without scale feature

Innovative Concept I am currently working on developing a unique carousel design. The main concept is to have the image gradually zoom in at the beginning of each carousel "page". The Challenge In order to achieve the desired outcome, I experimented wit ...

Guide to clicking on a user and displaying their details in a different component or view using Vue.js router

Currently working on a Vuejs project that requires the following tasks: Utilize an API to fetch and display a list of users (only user names) on the home page. Create a custom search filter to find users based on their names. Implement functionalit ...

Preventing "Access-Control-Allow-Origin" Error when sending requests from Firebase hosting to Firebase cloud functions

When I use the post function from Firebase Cloud Functions and send a post request from my React app hosted on Firebase Hosting, I encounter the following error in the console: Access to XMLHttpRequest at 'https://asia-east2-example.cloudfunctions.net ...