0

js1.js

    app.controller('test1Controller', 
    function($scope,$http,$ngBootbox,$location,CRUDService,NotificationService,constants,ngWizard) {


    $scope.fun1 = function(){

        $http.get(context+"/back/demande/rest/test1").success(function(data, status) {   
            $scope.dto = data;
        });
    };

    });

js2.js

    app.controller('test2Controller', 
    function($scope,$http,$ngBootbox,$location,CRUDService,NotificationService,constants,ngWizard) {


    $scope.fun2 = function(){

        $http.get(context+"/back/demande/rest/test2").success(function(data, status) {   
            $scope.dto = data;
        });
    };

    });

How can I call fun1 => js1.js in js2.js?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ayoubmrk
  • 9
  • 2
  • Does this answer your question? [Calling a javascript function in another js file](https://stackoverflow.com/questions/25962958/calling-a-javascript-function-in-another-js-file) – popcorn Jan 07 '20 at 18:25
  • If the method should be shared, why not put it in a service that is injected into both controllers? – Taplar Jan 07 '20 at 18:30

1 Answers1

0

First, you need to move your function to angular.js service instance. Then you need to inject this service to your controllers, like NotificationService. Then you can call in different controllers.

app.service('myHttpService', ['$http', function($http) {
    this.getData = function(context, endpoint) {
        return $http.get(context+"/back/demande/rest/" + endpoint);
    };
}]

// do not forget to use injector if you don't have ngAnnotate 
app.controller('test2Controller', function($scope, $http, $ngBootbox, $location, CRUDService, NotificationService, constants, ngWizard, myHttpService) {
    $scope.dto = null;

    $scope.fun2 = function(){
        myHttpService.getData(context, 'test1')
            .then(function(data, status) {   
                $scope.dto = data;
            });
    };

    });
Ihor Yanovchyk
  • 768
  • 6
  • 13