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?

2
  • Does this answer your question? Calling a javascript function in another js file Commented Jan 7, 2020 at 18:25
  • If the method should be shared, why not put it in a service that is injected into both controllers? Commented Jan 7, 2020 at 18:30

1 Answer 1

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;
            });
    };

    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.