Using Angular JS, the JSON method is invoked from a location external to the controller

How can I call an inner method from outside a controller in AngularJS?

var app;
(function (){
app = angular.module("gallery", []);

app.controller("galleryController", ['$scope','$http', function($scope, $http){
    var gallery = this;
    gallery.data = [];

    gallery.getJson = function(){
        $http.get('/urltojson/main-hero.json').success(function(data){
            gallery.data = data;
        });
    }

    this.getJson();
}]);  })();

Is it possible to call the getJson method from outside the controller?

Answer №1

To access the scope of an element within a specific controller in Angular, utilize the angular.element method.

For instance:

<div ng-controller="appController"><p id="example"></p></div>

You can then execute:

var newScope = angular.element( document.querySelector( '#example' ) ).scope();
newScope.getData();

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

Encountering an exception while parsing JSON

I'm trying to extract specific fields from a JSON object. Here is an example of the JSON data- { "id": "100000224942080000", "name": "Tech Geeky", "first_name": "Tech", "last_name": "Geeky", "link": "https://www.facebook.com/tech.geeky ...

API requests seem to be failing on the server side, yet they are functioning properly when made through the browser

My current project involves utilizing an API that provides detailed information about countries. I've set up an express server to handle requests to this API, but for some reason it's not making the request. Interestingly, when I directly access ...

AssistanceBubble.js with ASP.NET UpdatePanel

Looking for guidance on implementing HelpBalloon.js () within an ASP.NET UpdatePanel. Experiencing issues with image loss after a postback. ...

What causes the immediate firing of the DOM callback in Angular 1.2.9?

Check out a live demo here View the source code on GitHub here Angular 1.2.9 brought in DOM callbacks with the introduction of $animate:before and $animate:after events triggered during animations. However, it seems that the $animate:after event is trigg ...

The sequence of data and the final event triggered by http.createServer

const server = http.createServer(async (request, response) => { if (request.method === "POST") { var data = ""; request .on("data", async (chunk) => { console.log("1"); data + ...

Obtaining Local JSON data in a standard Flutter class

In the Services-> math_logic.dart file, I have a page dedicated to mathematical calculations where I did not use StateFul Widget. However, I encountered an error indicating that 'Future is not a subtype of String'. It seems that there was also ...

Special characters in JSON response

I received a JSON from an API with characters "\ u0083", "\ u0087d" and "\ u008d". I tried changing the encoding to utf-8 and ISO-8859-1, but without success. Can anyone help me out with this issue? It's important to note that the API c ...

Error: The function expressValidator is not recognized in the current environment. This issue is occurring in a project utilizing

I am currently working on building a validation form with Express and node. As a beginner in this field, I encountered an error in my console that says: ReferenceError: expressValidator is not defined index.js code var express = require('express& ...

I am having trouble getting the Express Router delete method to function properly when using mongoose with ES8 syntax

I was working on this code snippet : router.delete('/:id', (req, res) => { Input.findById(req.params.id) .then(Input => Input.remove().then(() => res.json({ success: true }))) .catch(err => res.status(404).json({ success: f ...

execute field function prior to sorting

Currently, I am building a graphql server in express and using a resolver to modify my fields based on user input from the query. The issue arises from the transformer function returning a function. My goal is to sort the results by a field determined by ...

Tips for implementing JWT in a Node.js-based proxy server:

I am a Node.js beginner with a newbie question. I'm not sure if this is the right place to ask, but I need ideas from this community. Here's what I'm trying to do: Server Configurations: Node.js - 4.0.0 Hapi.js - 10.0.0 Redis Scenario: ...

Eliminate every instance using the global regular expression and the replace method from the String prototype

function filterWords(match, before, after) { return before && after ? ' ' : '' } var regex = /(^|\s)(?:y|x)(\s|$)/g var sentence1 = ('x 1 y 2 x 3 y').replace(regex, filterWords) console.log(sentence1) sentence2 ...

Google Cloud Messaging (GCM) sends notifications displaying JSON messages

I'm currently struggling with creating GCM notifications. The default behavior is to display the message in JSON format. What I want to do is wrap my JSON data and only show a simple message like 'You have a new notification' in the notific ...

Jackson's @JsonAppend annotation with a predefined default value

Hey all! I'm currently in the process of building a web application and have chosen Jackson as my JSON processing framework. In terms of the request data I want to send, imagine my POJO structure like this: data class JSONEnvelope( @JsonProp ...

Tips for implementing try-catch with multiple promises without utilizing Promise.all methodology

I am looking to implement something similar to the following: let promise1 = getPromise1(); let promise2 = getPromise2(); let promise3 = getPromise3(); // additional code ... result1 = await promise1; // handle result1 in a specific way result2 = await ...

retrieve the date value of Focus+Context by using the Brushing Chart

Currently, I am engaged in analyzing the sentiment of tweets on Twitter. The analysis will produce an overall area graph that allows me to choose a specific date range and extract all or some of the tweets falling within that range. In order to create a t ...

Despite receiving a 200OK response, the API in GetStaticProps() is not providing any data

I'm trying to send an axios.get request to an API within getStaticProps() in next.js. Although the response is coming back with a 200OK status, I can't seem to locate the data in the response. Where should I be looking for this data? The data ...

Strategies for handling numerous node projects efficiently?

Currently, we are utilizing three distinct node projects: Project 1, Project 2, and Project 3 incorporating react and webpack. Each of these projects is stored in their individual repositories. While Project 1 and Project 2 operate independently, Project ...

Unable to retrieve data from the array

I am encountering an issue while trying to fetch data from an array, as I keep receiving undefined Please refer to the image for a visual representation of my problem. I'm not sure what I might be overlooking, so any help would be greatly appreciate ...

Utilizing AngularJS to upload numerous independent attachments to CouchDB

My goal is to upload multiple files to a couchdb document using angularjs. Despite my efforts with an angular.forEach loop, I am facing issues as the asynchronous $http calls do not wait for the previous loop to finish before moving on to the next one. Her ...