Retrieving the name of the view from within the controller

I am currently working with this state configuration:

  .state('main.content', {
    url: '',
    views: {
      'content-one': {
        templateUrl: 'templates/staging-area/content.html',
        controller: 'ContentCtrl'
      },
      'content-two': {
        templateUrl: 'templates/staging-area/content.html',
        controller: 'ContentCtrl'
      }
    }
  })

Is there a way to retrieve the name of the view within the controller?

Answer №1

To retrieve state configuration objects, you can utilize the helper method $state.get(). If you omit the route name as the first parameter, it will return the complete configuration for all routes.

app.controller('AppCtrl', function ($state) {

  var stateConfig = $state.get('main.content');

});

An alternative approach is to set a container for a view by specifying its name directly like

<div ui-view="content-one"></div>
. You can then query this element that sets a view using the following:

var element = angular.element('[ui-view]');
var name = element.attr('ui-view'); // 'content-one'

It's important to note that there may be multiple views on a page, so a setup like this would be more secure:

<div class="main-content" ui-view="content-one"></div>

JavaScript code:

var element = angular.element('.main-content');
var name = element.attr('ui-view'); // 'content-one'

Answer №2

To retrieve the current viewName, you have the option of utilizing either the $viewcontentLoading or $viewContentLoaded event. Check out this code snippet for reference:

$rootScope.$on('$viewContentLoading', function(event, viewName, viewContent){
    console.log('current view name : ', viewName);
});

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

What's the reason my JavaScript isn't functioning properly?

Brand new to coding and I'm delving into the world of Javascript for the first time. In my code, there's a checkbox nestled within an HTML table (as shown below). <td><input type="checkbox" id="check1"/> <label th:text="${item.co ...

Avoid file compression when utilizing node-uglify

Currently, I am utilizing uglify-js to consolidate all JS files into one and generate a map for them. However, I am facing issues in Angular due to code compression causing problems with variable name changes (this is definitely something that needs to be ...

Is it possible for AWS Lambda to store global variables?

I've devised a basic increment counter shown below. global.counter = 0; exports.handler = (event, context, callback) => { // TODO implement callback(null, ++global.counter); }; Every time I test this function, the value increments as anti ...

Utilizing jQuery on the container element but excluding the nested child elements

Imagine I have the following HTML structure: <div class="parent"> <div class="child1"></div> <div class="child2"></div> </div> How can I make jQuery execute a function when .parent is clicked on, but only if .c ...

Having trouble with React and Webpack's Extract Text Plugin. I can't seem to get my styles to work from bundle.css, even though they are functioning fine from bundle.js. Any advice on

Currently, I am working on my SCSS styles and using CSS modules to keep each component's styles separate. However, I have encountered an issue with generating a separate CSS bundle for the production build. While I am able to generate a separate CSS ...

Sending a request from one Node.js application to another Node.js application

I have a nodejs-express server (1) that connects to a mongodb and a web server (2) in nodejs-express with Angularjs. I am attempting to send a post request from 1 to 2, but I keep receiving a 405 Method Not Allowed error (I tried using Postman). When I che ...

Server-side-rendering is encountering an issue with a package that is restricted to running solely in a browser

I have integrated localForage into my SSR Nuxt project. and within my Nuxt-Page-Component ... <script> import localforage from 'localforage'; export default{ mounted(){ localforage.getItem('something', value => { ...

Changing the Displayed Content with a Simple Click of a Link

Layout Overview In the process of developing a tool to streamline work tasks, I want to ensure that I am following best practices. My goal is for the website to initially display only column A, with subsequent columns generated upon clicking links in the ...

Deactivate background hover effects for bar charts in Recharts

Is there a way to disable the gray background that appears when hovering over bar charts in Recharts? I'm using version 1.4.1 and my code looks like this: import React from "react" // Recharts import { Bar, BarChart, CartesianGrid, ResponsiveContain ...

What could be causing my Angular JS application to malfunction?

Presenting myFirstAngularApp.html <html ng-app="store"><!-- The "ng-app" attribute initializes an Angular app. Everything inside this tag is considered part of the Angular app due to ng-app --> <head> <link rel="stylesheet" type= ...

Filtering JSON data by nesting checkboxes in an AngularJS application

Trying to create a nested filter search in Angular with JSON data. Seeking assistance from the community since I am new to this and encountering difficulties. ...

Angular Material Slider is failing to display properly

I am struggling to get the Angular Material Slider to render correctly in my project. Here is the code I have been using: <div class="row formField" ng-cloak> <div class="col-md-2"> <div>送貨日期</div> & ...

Issue with jQuery Master-Detail dropdown list selection

Having trouble isolating the code in this jsfiddle script. Despite my efforts, it seems that isolation is not working as expected and Firebug isn't showing any errors on the console: <!DOCTYPE html> <html lang="en"> <head> ...

I'm trying to figure out how to incorporate types for utilizing Intl.ListFormat in node v12. Can

I am currently working with nodeJS version 12.10.0, which now includes support for Intl.ListFormat. I am also using Typescript version 3.6.3. However, when compiling my code with Typescript, I encounter the following error: Property 'ListFormat' ...

Implementing server-side pagination with Ngrx and using router parameters to navigate pages

In my store, I have a simplified state tree structure: { routerReducer: { state: { url: '/blog' }, queryParams: { category: 'home' } params: { } }, blog: { ...

Enclosing values in JavaScript literals

I apologize for the inconvenience. I am unsure why it is not functioning properly. If I input <button type="button" onclick="document.getElementById("demo").innerHTML = Date()">click</button> the above code does not work. However, if I u ...

Issue with JavaScript JSON.parse converting string values to Infinity

Does anyone have an explanation for this peculiar behavior I encountered while using the JSON.parse() function in Javascript? Normally, when you pass a string to it, it should generate an error. For example, JSON.parse("5ffc58ed1662010012d45b30" ...

How can one effectively bounce back from a promise that has gone unfulfilled?

As I delve into JS development, I've come across the DRY (Don't Repeat Yourself) concept, which has significantly helped me streamline my code. I'm facing a recurring issue in my project where I find it challenging to enhance without compro ...

I'm looking to create a unit test for my AngularJS application

I am currently working on a weather application that utilizes the to retrieve weather data. The JavaScript code I have written for this app is shown below: angular.module('ourAppApp') .controller('MainCtrl', function($scope,apiFac) { ...

Create a new object variable within an Angular2 or JavaScript application

Having experience with Angular 1, I used to organize my variables like this: var options = {}; options.location = "test"; options.time = "today"; Transitioning to Angular 2, I want to continue with the same practice of grouping variables by adding them t ...