Setting up an abstract state as the backdrop in AngularJS: A step-by-step guide

I am trying to maintain an animated background while switching between different states in my UI-router setup. The goal is for the background to remain constant while only the content of the states changes.

Here is how I have set up my UI-router and attempted to achieve this (unsuccessfully):

abstract.html

<ion-view class="content-back">

<!-- animated background -->
<div id="galaxy">
  <div class="bg"></div>
  <div class="stars-back"></div>
  <div class="stars-middle"></div>
  <div class="stars-front"></div>
  <div class="bg center"></div>
</div>    

<ion-nav-view name="menuContent"></ion-nav-view>

app.js

angular.module('starter', [
  'ionic', 
  'starter.controllers', 
  'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }
  });
})

and state config part

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider


   .state('abstract', {
      url: '/abstract',
      abstract: true,
      templateUrl: 'templates/abstract.html',
   })

   .state('tab.abstract', {
      url: '/dash',
     views: {
       'menuContent': {
          templateUrl: 'templates/tab-dash.html',
          controller: 'DashCtrl'
        }
      }
   })



  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/dash');

});

Answer №1

If we decide to create a state with an abstract state (called 'abstract'), each child of that state must notify UI-Router that it will be a parent:

.state('dash', {
    parent: 'abstract'
    url: '/dash',
    templateUrl: 'templates/tab-dash.html',
    controller: 'DashCtrl'
})

Instead of using parent: 'abstract', another approach would be to include it in the state name like this: .state('abstract.dash', {

Subsequently, the child state will be injected into the parent's view template, so it needs to have a designated ui-view=""

<ion-view class="content-back">

    <!-- animated background -->
    <div id="galaxy">
    <div class="bg"></div>
    <div class="stars-back"></div>
    <div class="stars-middle"></div>
    <div class="stars-front"></div>
    <div class="bg center"></div>

     // for example, here
     <div ui-view=""></div> // placeholder for child state

    </div>    
</ion-view>

The information provided above pertains to UI-Router state nesting: Nested States and Nested Views

Answer №2

For more information on this topic, you can refer to the following question and answer: Dealing with Ionic routing problems resulting in a blank page

I have modified the original plunker and made adjustments according to our requirements here

Let's define these states:

  .state('app', {
  //url: "/app",
  abstract: true,
  templateUrl: "tpl.tabs.html",
  controller: 'AppCtrl'
})
.state('home', {
    parent: 'app',
    url: "/app",
    ...
  })
.state('menu', {
    parent: 'app',
    url: "/menu",
    ...
})

The abstract state 'app' is already being used with the template "tpl.tabs.html":

<ion-tabs class="tabs-icon-top">

  <ion-tab title="Home" icon="icon ion-home" href="#/app">
    <ion-nav-view name="home"></ion-nav-view>
  </ion-tab>

  <ion-tab title="Dash" icon="icon ion-person" href="#/dash">
    <ion-nav-view name=""></ion-nav-view>
  </ion-tab>

  <ion-tab title="Menu" icon="icon ion-person" href="#/menu">
    <ion-nav-view name="menuContent"></ion-nav-view>
  </ion-tab> 

</ion-tabs>

This setup is prepared for the new states we are adding:

.state('abstract', {
  parent: 'app',
  abstract: true,
  templateUrl: 'templates/abstract.html',
})
.state('dash', {
    parent: 'abstract',
    url: '/dash',
    templateUrl: 'templates/tab-dash.html',
    controller: 'DashCtrl'
})

It's important to note that 'dash' not only has 'abstract' as its parent but also 'app' (indicating a deep hierarchy)

Here is the content of the parent 'abstract':

<ion-view class="content-back">

    <!-- animated background -->
    <div id="galaxy">
    <div class="bg"></div>
    <div class="stars-back"></div>
    <div class="stars-middle"></div>
    <div class="stars-front"></div>
    <div class="bg center"></div>

    <ion-nav-view name=""></ion-nav-view>
    </div>

</ion-view>

You can see it action here

Answer №3

Check out the code snippet below. The "templates/abstract.html" template will remain persistent in the background, while "templates/tab-dash2.html" and "templates/tab-dash2.html" will be loaded as part of the background process. Direct access to the "/abstract" URL is not permitted. However, you can access tabs using the URLs "/abstract/dash1" and "/abstract/dash2".

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which utilizes states
  // Find more info here: https://github.com/angular-ui/ui-router
  // Establish the different states that the app can exist in.
  // The controllers for each state are located in controllers.js
  $stateProvider

   .state('abstract', {
      url: '/abstract',
      abstract: true,
      templateUrl: 'templates/abstract.html',
   })

   .state('abstract.tab1', {
      url: '/dash1',
     views: {
       'menuContent': {
          templateUrl: 'templates/tab-dash1.html',
          controller: 'DashCtrl1'
        }
      }
   }).state('abstract.tab2', {
      url: '/dash2',
     views: {
       'menuContent': {
          templateUrl: 'templates/tab-dash2.html',
          controller: 'DashCtrl2'
        }
      }
   })

  // In case none of the above states match, use this as the default
  $urlRouterProvider.otherwise('/dash');

});

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

The XMLHttpRequest response states that the preflight request did not meet the access control check requirements

I am attempting to subscribe to a firebase cloud messaging topic by sending an http post request. var data = null; var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState ...

Data extracted from the range selector

Is there a way to take values and use them for hiding or displaying certain divs? I have been using jQuery Simple Slider for this purpose. I attempted the following code, but it did not work well. I want the div with id "3" to be visible when the slider ...

Preventing Body Overflow Without Affecting Iframe Overflow in Meteor.js

Currently, I am working on a project using Meteor for a Point of Sale (POS) system. For those unfamiliar with it, Meteor is a framework that allows for the use of JavaScript, jQuery, and various other web app scripting languages. The goal of this applicati ...

Is there a way to incorporate innerhtml (or innertext) with the h() function for adding content?

Due to certain requirements, I need to utilize virtual DOM. Below is the code snippet that I am working with: // test.js import { h, ref } from 'vue' const ButtonCounter = { name: 'button-counter', props: { tag: { type: S ...

Is it possible to efficiently transfer a Tensorflow.js Tensor between Node.js processes?

Currently, I am in the process of building an AI model using Tensorflow.js and Node.js. One of the challenges I am facing is handling a large dataset in a streaming manner due to its size being too massive to be stored in memory all at once. This task invo ...

Troubleshooting Cordova's ng-route functionality issue

I am currently working on an Angular application that includes the following code: // app.js var rippleApp = angular.module('rippleApp', ['ngRoute', 'ngAnimate', 'ngAria', 'ngMaterial']); // configure ou ...

Displaying a specific division solely on mobile devices with jQuery

I need to implement a feature where clicking on a phone number div triggers a call. However, I only want this div to be displayed on mobile devices. To achieve this, I initially set the div to "display:none" and tried using jQuery to show it on mobile devi ...

Steps to run a function for updating a JSON data file

I currently have a Leaflet map displaying weather data retrieved from a Json source. There is already a function in place that updates the data every x minutes using setInterval. setTimeout(function () { refreshId = setInterval(function () { ...

Accessing information from JSON files using AJAX

I'm currently working on loading data from a JSON file using AJAX. The file I'm referencing is external-file.json. Within the code snippet provided, there are other unrelated sections. I'm encountering an issue within the getViaAjax function ...

Connecting onClick event to a dynamically created div using Dojo

While working with dojo 1.9.2, I encountered an issue when trying to add an onClick function to a dynamically created piece of HTML code like so: clickableDiv = "<div data-dojo-attach-point=\"testBtn\">Click Me!</div>"; self.racks.in ...

I'm curious why one of my Material UI autocomplete components is displaying options with a blue background while the other isn't. Take a look at the code sandbox for more insight

UPDATE: Problem solved! I managed to find a solution and have updated my sandbox with the fix! REVISION: After some investigation, I have identified that the issue lies within this specific line of code in the autocomplete... isOptionEqualToValue={(option ...

Using AngularJS to present text based on a boolean value

I'm working with HTML and AJS where I am trying to display the value Is Boss? : true based on the boolean value of person.IsBoss. <span>Is Boss? :</span> <span> {{ person.IsBoss }}</span> However, instead of displaying true o ...

The navbar tag is not functioning properly within Reactjs

Need help with creating a web header in ReactJS? I'm still learning Javascript and struggling to position the menu on the right side using bulma CSS. Can anyone offer some guidance? import React, { Component } from 'react'; import './H ...

Creating a Cross Fade Animation effect with the combination of CSS and JavaScript

I've been attempting to create a similar animation using html and css. Below gif shows the desired outcome I am aiming for: https://i.sstatic.net/YsNGy.gif Although I have tried the following code, I have not been able to achieve the desired result ...

The handler for errors in DRW consistently generates the message "Error" instead of providing specific information about the issue at hand

FiltersManager.getAllServices({ callback : updateServiceFilter, errorHandler : function(message) { alert(message); } }); Although I throw an exception when an error occurs in th ...

"Successful implementation of Ajax function in local environment but encountering issues when running online

I am facing an issue with my AJAX function. It works perfectly fine on my local server but does not return anything when I move it to my online server. Below is the code snippet: This is the part of the page where I call the showEspece() function: echo ...

The texture fails to display upon loading the .dae model

I'm attempting to load a .dae model in three.js, but it seems like the texture is not loading. Here is my code snippet: <script src="js/three.js"></script> <script src="js/Animation.js"></script> <script src="js/An ...

Encountering problems when trying to mock values with Jest

I'm currently integrating Jest into an existing project that is already utilizing enzyme and jasmine. Although I have installed the jest packages and configured them initially, I am facing an issue where the mock data is not being supplied correctly. ...

What is the best way to implement Axios for data fetching in Gatsby's useStaticQuery function?

One way to fetch data in Gatsby is by using GraphQL, like in the following example: import { graphql, useStaticQuery } from "gatsby" const IndexPage = () => { const gatsbyRepoData = useStaticQuery(graphql` { github { repo ...

Why isn't the jQuery click() function functioning on my modified HTML?

I am trying to create a unique version of the go-moku game using different programming languages and databases. My aim is to enhance the game's functionality by incorporating jQuery, PHP, and a MySQL database. var moveCount = -1; setInterval(function ...