Tips for showing the results of a function from a controller in a nested view using AngularJS

My main view is called index.html

 <html>
  <head>
    <script src = "assets/js/angular.min.js"></script>
    <script src = "../lib/js/angular-ui/angular-ui-router.js"></script>
    <script src = "app.js"></script>
    <script src = "controllers/profile/ProfileController.js"></script>
  </head>

  <body ng-app="myApp">
     <div class="links">
       <div class='profile'><a ui-sref="profile">Profile</a></div>
       <div class='dashboard'><a ui-sref="dashboard">Dashboard</a></div>
     </div>
     <div class="container">
        <div ui-view></div>
     </div>
   </body>
   </html>

In my app.js

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

   app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('profile', {
            url: "modules/profile/templates",
            templateUrl: "modules/profile/templates/profile.html",
            controller: 'ProfileController'
        }).state('dashboard', {
            url: "modules/dashboard/templates",
            templateUrl: "modules/dashboard/templates/index.html"
        }).state('profile.viewprofile', {
            url: "modules/profile/templates",
            templateUrl: "modules/profile/templates/viewprofile.html",
            controller: 'ProfileController'
        }).state('profile.createprofile', {
            url: "modules/profile/templates",
            templateUrl: "modules/profile/templates/createprofile.html",
            controller: 'ProfileController'
        });
    }
]);

This represents my profile.html

 <div >
   <div class="links">
    <h3 class="heading">Profile</h3>
       <div class='viewprofile'><a ui-sref="profile.viewprofile">View Profile</a></div>
       <div class='createprofile'><a ui-sref="profile.createprofile">Create Profile</a></div>
       <div class='editprofile'><a ui-sref="profile.editprofile">Edit Profile</a></div>
    </div>
    <div class="content>
       <div ui-view="profile.viewprofile@"></div>
       <div ui-view="profile.createprofile@"></div>
     </div>
   </div>

Inside my ProfileController

app.controller('ProfileController',function($scope, $http){
        $scope.allProfiles = function(){
       $http.get('objects/getallprofiles.php').success(function (data, status, headers, config) {
    console.log(data);
    });
}

When viewprofile link in the profile.html page is clicked, the viewprofile.html is rendered. How can I pass the output of the allProfiles() function from the controller to the child view viewprofile.html and bind them to html tags?

What steps should be taken to display the results of the allProfiles() function, which is located in the controller, in the child view viewprofile.html

Answer №1

To pass data from the allProfile() function to a rootScope variable, simply assign it like this:

$scope.allProfiles = function(){
       $http.get('objects/getallprofiles.php').success(function (data, status, headers, config) {
      $rootScope.profiles=data;
    console.log(data);
    });

Then, you can access this variable in any controller by using $rootScope.

Remember to inject $rootscope into your controller.

Another approach is to save the data in a service and inject that service into the desired controller for data access.

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 is the best way to receive a response from the foo.onload = function() function?

My current code involves a function that is triggered by .onload, and I'm looking to return a specific value based on certain conditions: newImg.onload = function() { var height = newImg.height; var width = newImg.width; if(height > wi ...

Troubleshooting: Express JS and MongoDB Post Request Issue

Today, I delved into building a cutting-edge MERN App and embarked on crafting a restful api. To kick things off, I opted to utilize mlab as the storage solution for my mongodb database. Following the creation of a user, I successfully established a connec ...

Ways to make an AngularJS Expression show an empty value

Consider the following HTML snippet: <p>{{ booleanVariable ? "The variable is true!" : "" }}</p> In case 'booleanVariable' evaluates to false, the expression should display nothing and the <p></p> tags should not be show ...

Guide to displaying the output of a JS calculation in a Bootstrap modal dialog box

I have a HTML and JavaScript code that determines the ideal surfboard for the user based on their input data (style, experience, height, weight) and displays the recommended surfboard type. Here is the initial code snippet I attempted to use: function c ...

What is the process ShaderToy uses to load sound files directly into a texture?

I'm attempting to replicate the functionality of shadertoy in passing audio frequency and waveform into a shader using three.js. In this specific example, it appears that IQ is converting audio data into an image which is then utilized as a texture i ...

Is "content_scripts" malfunctioning in Chrome version 38 and above?

Just recently, I created a Chrome Extension with the purpose of making all webpages utilize a specific font. Everything was functioning perfectly fine until yesterday (or possibly even earlier). The crucial codes for this extension are: manifest.json fil ...

Unexpected behavior from Internet Explorer - Span contents remain unchanged despite valid input

I have a simple question because I'm feeling a bit lost. Check out this JSFiddle link It seems that in Internet Explorer, the contents of my span won't update even though the input is valid. However, in other browsers, the span content changes ...

Discover the magic of AngularJs autocomplete with a focus on exploring

I am currently utilizing autocomplete feature in my AngularJS application using NgAutocomplete module from . I would like to retrieve the continent of the selected city, but I'm not sure how to achieve this. Upon making a request, I receive a response ...

Exploring JavaScript: Uncovering the Secrets of x and y Coordinates

Is there a more efficient way to find the x and y coordinates of various points on a map designed in Adobe Illustrator that is 600px by 600px, such as a pizza store? Rather than relying on trial and error for each point, I'm looking for a quicker and ...

Creating Circular Trails during Animation in D3.js

Currently, I am creating circles with each update that smoothly transition from one position to another. Below is the code snippet I am utilizing... function drawChart(newData) { var circles = slider.selectAll(".dot") .data(newData); circl ...

The Node.js express server works perfectly fine when running locally on localhost, but encounters difficulties when deployed on the

My Express.js code is functioning properly on localhost, but I'm encountering issues when deploying it to Heroku. It seems like there may be a bug in the code. Here's a glimpse of my code and the errors I've encountered: server.js const exp ...

Is there an equivalent of gsub in JavaScript?

Is there a way to achieve functionality similar to Ruby's gsub in JavaScript? I am working with a local HTML file and need to replace specific template variables with content, but I am struggling to find a solution. The HTML code includes elements lik ...

Invalid for the purpose of storage

Encountering the following error message: The dollar ($) prefixed field '$size' in 'analytics.visits.amounts..$size' is not valid for storage. return Manager.updateMany({}, { $push: { & ...

"Troubleshooting: Why isn't my MVC5 Controller able to receive

I am facing an issue with a controller method that I have defined: public JsonResult Save(List<BlogInfo> list) { return Json(new { Data = "" }, JsonRequestBehavior.AllowGet); } In addition, there is an ajax post request from the client side lik ...

What is the best way to ensure that the value of a <textarea> in jQuery is consistently saved and reflected in the HTML document?

I have a function on my website that allows users to input text which is then displayed on the page. However, this text disappears when the page is reloaded and does not stay permanently. How can I use jQuery to make sure that the entered text remains on ...

Load page content dynamically with Ajax in a specific div while still allowing the option to open the content in a new tab by right-clicking

As I work on developing a new website that utilizes a MySQL database to sort various items into categories and subcategories, I have implemented a script that dynamically loads category content into a div without requiring a page reload. This seamless load ...

Managing sessions across multiple tabs

Encountered this issue recently: Suppose I have two tabs open, Tab A and Tab B. I login with Tab A, authenticate, and redirect to the home page. Then, instead of entering a link directly, I right-click and select "Open link in new tab" resulting in Tab B. ...

Duplicate user scrolling input within a specified div container

I am attempting to recreate a horizontal scrolling effect on a div element that mirrors the input scroll. When the user scrolls along the input, I want the div element to scroll in sync. The issue I am encountering is specific to Chrome, where the input b ...

Tips on preventing right-click actions in jqGrid

While utilizing onSelectRow in a jqGrid, I have noticed that it functions properly when clicking with the left mouse button. However, when right-clicking, it still triggers the function. My aim is for the right-click to perform its usual action (such as di ...

Creating a visually striking layout with Bootstrap card columns masonry effect by seamlessly adjusting card heights to prevent any

When using the bootstrap card columns masonry and a user clicks on a button inside a card, the height of the card changes dynamically by adding a card-footer. However, in certain situations, the cards change position causing a jumpy effect. To see this i ...