0

i have this code, which gets a json, inside a controller i need to know how am i able to call inner method, from outside controller.

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 getJson from outside the controller?

1 Answers1

1

Use angular.element and get the scope() of the element that resides within the controller you want to call methods on.

Example:

<div ng-controller="galleryController"><span id="test"></span></div>

You can do:

var scope = angular.element( document.querySelector( '#test' ) ).scope();
scope.getJson();
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • var a = angular.element("#gallery").scope() that doesn´t return getJson method, dunno why. html is – Sebastian Uriel Murawczik Jul 29 '14 at 16:00
  • ok, got how to call the json method, but the two way data binding somehow is not working, the process for this is, i have a json, which is loaded when page loads, i have the hability to change json without refreshing page, problem is that when i refresh json, this happens for ex

    {{gallery.data.lowertext}}

    – Sebastian Uriel Murawczik Jul 29 '14 at 16:03