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
110k241 gold badges160 silver badges233 bronze badges
-
Does this answer your question? Calling a javascript function in another js filepopcorn– popcorn2020年01月07日 18:25:40 +00:00Commented 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?Taplar– Taplar2020年01月07日 18:30:03 +00:00Commented Jan 7, 2020 at 18:30
1 Answer 1
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;
});
};
});
answered Jan 7, 2020 at 18:53
Ihor Yanovchyk
7966 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js