[英]how can i call function js in another file js ANGULARJS
js1.js 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 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?如何在 js2.js 中调用 fun1 => js1.js?
First, you need to move your function to angular.js service
instance.首先,您需要将您的函数移动到 angular.js
service
实例。 Then you need to inject this service to your controllers, like NotificationService
.然后你需要将这个服务注入到你的控制器中,比如
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;
});
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.