retrieving information returned by a state's resolve function

I am currently working with an AngularJS module called ui-router and making use of the resolve property to retrieve the id of a specific item.

In another file, I have a component named patent for a state known as patents.patent. This component contains a function that returns the id. My goal is to be able to access this returned id from the controller in order to fetch the relevant data associated with the selected item.

Can someone guide me on how to access the value returned by the resolve function from the controller?

var app = angular.module('myApp', ['ui.router', 'chart.js']);

app.config(['$stateProvider', function($stateProvider) {

    $stateProvider
    .state('patents', {
        url: '/patents',
        component: 'patents',
        resolve: {
            patents: function(patentsService) {
                return patentsService.fetchAllPatents();
            },
            graphs: function(patentsService) {
                return  patentsService.fetchGraphData();
            }

        }      
    })
    .state('patents.patent', {
        url: '/{patentId}',
        component: 'patent',
        resolve: {
            patent: function(patents, $stateParams) {
                return patents.find(function(patent){ 
                    return patent.id == $stateParams.patentId;
                })
            },
            graph: function(graphs, $stateParams) {
                return graphs.dataset.find(function(graph){
                    return graph.id == $stateParams.patentId; //NEED THIS VALUE
                })
            }
        }
    })

}]);

angular.module('myApp').component('patent', {
    bindings: { 
        patent: '=' ,
    },
    templateUrl: '../templates/patents/list/patent-item.htm',
    controller: function() {
        var vm = this;

        //HERE IS WHERE I NEED TO ACCESS THE RETURNED VALUE

    }
});

Answer №1

If you want to access the data that has been resolved, all you need to do is define them as bindings in your controller. Just like you did with patent. Then, you can access them using your vm variable (or this).

angular.module('myApp').component('patent', {
    bindings: { 
        patent: '<' ,
        graph: '<'
    },
    templateUrl: '../templates/patents/list/patent-item.htm',
    controller: function() {
        var vm = this;

        vm.$onChanges = function(changeObj){
            if(changeObj.patent){
                console.log('I am your patent', vm.patent);
            }
            if(changeObj.patent){
                console.log('I am your graph', vm.graph);
            }
        }
    }
});

@rrd has mentioned that it's important to protect your controllers from minification, but you should also ensure that your resolve functions are safeguarded against minification.

For example:

resolve: {
    patents: ['patentsService', function(patentsService) {
        return patentsService.fetchAllPatents();
    }],
    graphs: ['patentsService', function(patentsService) {
        return  patentsService.fetchGraphData();
    }]
} 

Answer №2

According to Vivz's suggestion, injecting into the controller should function properly, but it is essential to protect against potential minification issues:

angular.module('myApp').component('patent', {
  bindings: { 
    patent: '=' ,
  },
  templateUrl: '../templates/patents/list/patent-item.htm',
  controller: ['graph', function(graph) {
    var vm = this;

    //WHERE I MUST RETRIEVE THE VALUE RETURNED

  }]
});

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

Trouble with React Native ListView Item Visibility

I'm currently working on integrating the React Native <ListView /> component with the <List /> and <ListItem /> components from React Native Elements. However, I seem to be facing an issue where the <ListItem /> component is no ...

Verify the presence of the promotion code and redirect accordingly

I have created a special promotion page that I want to restrict access to only users who have received a unique code from me via email. To achieve this, I have designed the following form: <form accept-charset="UTF-8" action="promotion.php" method="po ...

Error: Attempting to assign value to the 'innerHTML' property of null in a Wordle clone with local storage

While developing a Wordle-inspired game for my website, I decided to utilize Local Storage to store the user's progress. Everything seemed to be working smoothly until an unexpected error occurred: "Uncaught TypeError: Cannot set properties of null (s ...

Display the selected item from the array while concealing the rest using React

I'm creating a Q/A page where questions are initially visible and answers are hidden. When a question is clicked, the corresponding answer should be displayed. I have an array of objects that I map through in JSX to display the questions. However, wh ...

Leveraging the flexibility of an undefined variable as a useEffect dependency

Summary: Need help using listRef.current.clientWidth as a dependency in useEffect. I'm working on creating a dynamic list that automatically adjusts the width of its items based on the list's width. I've almost got it working, but I'm ...

Displaying a single result in Angular from JSON without using the ng-repeat filter

Currently, I am working on developing an app to display news updates from a JSON API. The issue I am facing is that loading all the data from the JSON and filtering it using ng-repeat is causing slow performance, as there is a large amount of data. I woul ...

Is it possible to swap squares for circles in a Three.js PointCloud?

Currently, I am loading a .ply file using PLYLoader in three.js and displaying it as a PointCloud. Here is the code snippet: var loader = new THREE.PLYLoader(); loader.addEventListener('load', function (event) { var geometry = event.content; ...

Form a collection of visible table rows without hidden columns

My table allows users to filter out specific rows by selecting a checkbox. When a checkbox is checked, certain rows are hidden. I am trying to create an array with all the rows that are not hidden, but I am having trouble accessing the visibility state of ...

Enhance your cloud functions by updating data

Recently, I encountered an issue with a function I wrote that interacts with the Real Time Database. Every time I attempted to write data to a specific node, it would add the complete dataset and then promptly delete everything except one entry. https://i ...

Steps for retrieving the group names of properties in Autodesk Forge

Can someone please help me figure out how to obtain the property group names in Autodesk Forge? I have attempted to use getProperties() and getBulkProperties(), but have had no success in retrieving the group names. Any guidance on how to accomplish this w ...

Loop through XML nodes in Node.js

I am faced with a challenge of extracting specific data from a large XML document. The document can be accessed via the following link: https://pastebin.com/mNXWt7dz My goal is to parse the XML structure in order to retrieve values for each client-mac, cl ...

In Three.js, objects are consistently displayed in a hierarchical manner, with each one appearing on top

As a newcomer to JavaScript and Three.js, I managed to write some code that successfully renders a sphere representing Earth and a smaller sphere as a Satellite in orbit. However, a perplexing issue arises when the Satellite passes behind Earth – it rema ...

What is the purpose of creating a new HTTP instance for Socket.io when we already have an existing Express server in place?

As I delve into SocketIO, I've combed through various blogs and documentation on sockets. It seems that in most cases, the standard approach involves creating an HTTP server first and then attaching the socket to it as shown below: var app = express() ...

Once an ng-repeat is completed, I must extract and retrieve the 'id' of a specific element

Is it possible to retrieve the 'id' of the comment I'm replying to and save it for an Ajax call? I can easily access other data with ng-model, but using value="{{this.id}}" in a hidden input doesn't seem to work like in JQuery. <scr ...

Is there a way to refresh angular data without having to reload the page?

I have a database table with data that I am displaying using Angular. The script I am using is located in the footer section: <script !src=""> var app = angular.module('enterprise', []); app.controller('entercontroller', funct ...

Utilizing a single controller to manage multiple states in Angular using ui-router

Currently, I am in the process of learning AngularJS and developing a web application that utilizes ui-router. In my project, I have set up the states as shown below: angular.module('test', []) .config(function($stateProvider){ $stateProvider ...

Unit testing component in Ionic 2 with Ionic's specific markup and elements

Within my Angular 2 component for an Ionic 2 app, I utilize Ionic's markup as shown below: <ion-card> <h3>{{ rawcontent.name }}</h3> <p *ngIf="rawcontent.description">{{ rawcontent.description }}</p> </ion-car ...

Tips for Ensuring the Longevity of my Node.js Server

After developing an application in Node js with express js, I am facing an issue where the server shuts down whenever I close the command prompt. To start the app, I use the following command: c:/myApp > npm start I attempted to resolve this problem b ...

Guide to accessing object items in v-for in VueJs

Upon inspection, the following results are being displayed: https://i.sstatic.net/oF4Qe.png https://i.sstatic.net/21aHB.png In the Vue template code provided: <select class="form-select" v-model="post_format.wpml_language"> <option v-for="(l ...

Deciphering key-value pairs that are separated by commas

I am looking to convert the following format: realm="https://api.digitalocean.com/v2/registry/auth",service="registry.digitalocean.com",scope="registry:catalog:*" Into this JSON object: { realm: "https://api.digitaloce ...