Passing data from resolve to controller in Angular 1 using Ui-router can be achieved by implementing specific

Is there a way to pass the data contactName from the state to the controller using resolves?

.state({
      name: 'contact.detail.overlay',
      abstract: true,
      component: 'overlayContent',
      resolve: {
        contactName: (originalContact, $rootScope, ContactService) => {
          $rootScope.contactName = ContactService.getContactName(originalContact)
          return $rootScope.contactName
        }
      }
    })

Although everything seems correct in my state and with the contactName, I am unable to access it in my overlayContent controller. Is there a solution to have access to $rootScope.contactName in the controller?

EDIT: The controller :

class overlayContentComponent {

  constructor($state, $interpolate, $scope) {
    'ngInject'
    this.$scope = $scope
    this.$state = $state
    this.$interpolate = $interpolate
  }

  /**
   *
   * On initialization hook, let's check if an overlay title
   * has been defined for the current state.
   */

  $onInit() {
    const stateData = this.$state.current.data || { title: '' }
    this.overlayTitle = angular.isDefined(stateData.title) ? this.$interpolate(stateData.title)() : ''
  }
}

export const overlayContentComponent = {
  template: require('./overlay-content.html'),
  controller: overlayContentComponent
}

I have found that I can access contactName in my controller using this.$scope.$root.contactName, but I am looking for an alternative to avoid using $scope.$root.

Answer №1

Revise your constructor implementation:

constructor($stateProvider, $parse, $rootScope, nameOfContact) {
  'ngInject';
  // Update the variables accordingly:
  this.nameOfContact = nameOfContact;
  this.$rootScope = $rootScope
  this.$stateProvider = $stateProvider
  this.$parse = $parse
}

The controller should now receive the 'nameOfContact' resolve injection from where it resides in the state configuration. With this setup, you can utilize it within the controller as demonstrated above.

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

A guide on sending arguments to a react function component from a JSX component via onClick event handling

Below is a brief excerpt from my extensive code: import React from "react"; const Home = () => { return ( imgFilter.map((imgs) => { return ( < Col sm = "3" xs = "12" key ...

Interacting with Django backend via AJAX to trigger Python functions using jQuery/AJAX

Currently, I am developing a small website using Django. Within this project, there are DateTime fields that need to be handled. My intention is to utilize ajax by implementing another button to establish the UTC date while taking into consideration sola ...

Guide on converting a buffer to a PDF file using front-end technologies

I utilized Puppeteer to create a buffer containing pdf data and then transmitted it to the frontend application. Below is the code snippet for the backend API: exports.getPDF = async (req, res) => { const { content } = req.query; const browser = ...

Is there a way to execute a function from a separate controller in Angular in order to update the scope?

Is it possible to update a scope variable in the view by calling getData from a different controller in the example below? var app = angular.module('app', []); app.factory('MyService', ['$http',function($http) { return { ...

The computed function functions smoothly in one component but encounters issues in a different one

After adding multiple components to a page, I noticed that updates made to a parent object are reflected in one component but not the other. The main page, PatientEditor.vue, includes the following components: <notes-editor v-model="pt" /> <char ...

Stop jQuery ajax from running any JavaScript code contained in a script or HTML response

As stated in the documentation: If html is specified, any embedded JavaScript within the retrieved data will be executed before returning the HTML as a string. Similarly, using script will execute the pulled back JavaScript before returning nothing. Is ...

I'm curious to know where the router.get function is defined within the Express source code

Curious to find out how express.Router.get is implemented. After examining the express source code on git, I began with the project's index.js. The index file contains module.exports = require('./lib/express'), leading me to a file with va ...

Navigating to a different page with fresh data in Express.js

While browsing the homepage, I come across a list of names. Clicking on one of the names triggers a post request to the backend. At this point, my goal is twofold: firstly, fetch the corresponding profile data from the database; secondly, redirect to the p ...

Interpret the ng-model data into the input field

My challenge involves translating a value within an input. The input is disabled to prevent users from typing text into the textbox. Currently, the data is entered using ng-model and appears as shown below: <input ng-model="reason" ng-disabled="true" t ...

Inject a value sent from response.render directly into the script tag

Below you will find a pug snippet. I am looking for a way to dynamically insert the user value into the chatConfig object. script. var chatConfig = { user : 'foo', pass : 'bar', } When rendering from my Express applicatio ...

I am having trouble with my quiz function as it only checks the answer correctly for the first question. Does anyone have suggestions on how to make it work for

Currently, I'm tackling a quiz project that was assigned to me during my bootcamp. My focus right now is on the checkAnswer function, which evaluates the answer selected by the player. const startButton = document.querySelector(".start") as ...

Jquery code failing to trigger any response

Recently, I quickly created a jQuery script to dynamically populate a paragraph element in order to easily switch between player and server interaction options. However, I am currently facing an issue where my script does not populate as expected. I have a ...

What is the process for manipulating the value of an input element with Vue.js?

One of my challenges is setting initial values for input components upon creation and then retrieving the updated values after clicking a button. Setting the initial values works perfectly with this code: <template> <div class="editUserInfo"&g ...

How can I add a tilt to a sphere in Three.js?

I'm working on creating the moon - earth system in Three JS. For this, I want the moon sphere to have a tilt of 6.68 degrees and the earth sphere to be tilted at 23.5 degrees. Despite being new to Three JS, I couldn't find any information in the ...

What methods do languages use to manage the side effects caused by compound operators?

Let's consider a scenario like this: int a = (--t)*(t-2); int b = (t/=a)+t; While in C and C++, this leads to undefined behavior, as explained in this article: Undefined behavior and sequence points But how is this situation handled in: JavaScrip ...

Whenever I try to include something within the `componentWillUnmount` function,

I'm currently learning React on my own. I've been trying to save session data in my componentWillUnmount method. However, when I add code inside componentWillUnmount, nothing seems to happen. I tried debugging by adding console logs and debugger ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

I am new to javascript and jquery. I have encountered numerous cases involving audio players

Recently delved into learning javascript and jquery. I managed to create a basic audio player that plays short audio clips. However, I've encountered an issue where clicking the play button on one clip displays stop buttons on all clips instead of on ...

Is it possible to spice up functions passed as @Input in Angular with curry?

I have a set of modals with similar styling but completely different functionalities that I need to use in various scenarios within my app. To make it easier for me, I want to pass the logic as input in these different scenarios. When using simple function ...

Is there a way to displace a 2D triangle within a webgl shader?

I am facing a challenging situation while trying to implement conservative depth rendering, as described in CPU gems. My primary obstacle is enlarging the triangle, and it seems like I might have encountered the "stupid" phase. To illustrate the current s ...