如何从服务与在angularjs响应数据控制器重定向? [英] How to redirect from the service to the controller with the response data in angularjs?
问题描述
我需要一些帮助,我是新来的angularjs。
I need some help and I'm new to the angularjs.
我已经创建了旅游的应用程序,在这里,我的每一页三个控制器和从响应中获取数据一条龙服务。
I have created an app for tours, Here I have three controllers for each page and one service for getting data from the response.
我有四个按钮(链接看起来像西班牙,法国,印度,美国等)对我的每一页的页脚。
I have four button (links look like Spanish, France, India, America, etc.) on my each page footer.
我的问题是,而在主控制器(packageController)我能够访问响应数据,而无需重新定向,因为这是同一个控制器上的位置按钮点击,但我有一个控制器有1安培其它页面; 2,从这些控制器我需要重定向到主控制器,但我没能做到这一点。
My problem is while clicking on the location button in the main controller (packageController) I'm able to access the response data without redirecting because this is same controller, But I have other pages which have controller 1 & 2, from these controller I need to redirect to the main controller, but I'm failing to achieve this.
应用:
var app = angular.module("dashboard");
服务:
app.factory('packageService',function($http) {
return {
getData: function (location) {
var promise = $http({
method:'GET',
url:"index.php/tours" ,
params:{'keyword': location}
})
.success(function (data, status, headers, config) {
return data;
})
.error(function (data, status, headers, config) {
return {"status": false};
});
return promise;
}
}
});
主控制器:
这是工作
app.controller("packageController", function($scope, packageService){
$scope.footerLink = [
link1 : paris,
link2 : india,
link3 : america
];
$scope.goBtn = function(search){
packageService.getData(search.location).then(function(promise){
$scope.packages = promise.data;
});
};
}
控制器1
如果我点击从我的网站我打电话的页脚环节之一 goBtn()
,但不重定向。
If i click on one of those footer links from my site i'm calling goBtn()
, but is not redirecting.
app.controller("hotelController", function($scope, packageService){
$scope.footerLink = [
link1 : paris,
link2 : india,
link3 : america
];
$scope.goBtn = function(search){
packageService.getData(search.location).then(function(promise){
$scope.packages = promise.data;
});
};
}
2控制器
app.controller("flightController", function($scope, packageService){
$scope.footerLink = [
link1 : paris,
link2 : india,
link3 : america
];
$scope.goBtn = function(search){
packageService.getData(search.location).then(function(promise){
$scope.packages = promise.data;
});
};
}
的有关页面流Understaing,请参阅本下面的图片。的
注意:可能是我的做法将是错误的,请帮我
Note: Might be my approach will be wrong, Please help me!
推荐答案
尝试传递服务作为一个依赖也到控制器,如果还是不行,请尝试解决它说明如下:
Try passing the service as a dependency also to the controllers, if that doesn't work, try solving it as explained below :
我面临同样的问题。我解决了它是这样的:
I was facing the same problem. I solved it like this :
我做的模块不同的模块的每个页面,每个模块有它的控制器。的第一页的模块包含的服务。
使用在不同的控制器相同的服务,我通过第一模块(包含服务),为在其它模块的依赖关系,然后通过第一模块的服务为在控制器的依赖性和作为的函数的参数控制器。
I made module different modules for each pages, each module had a controller in it. The module of the first page contained the service. To use the same service in different controllers, I passed the first module (which contains the service) as a dependency in other modules and then passed the service of the first module as a dependency in the controller and as an argument in the function of the controller.
所以基本上我对含有与作为依赖传递的主模块和服务的控制器模块每个页面单独的JavaScript文件。
So basically I had a separate Javascript file for each page with a module containing a controller with the main module and service passed as dependencies.
请第一模块:
var app = angular.module('Dashboard', ['ngRoute']);
请中相同的方式,第一控制器
Make the first controller in the same manner.
请中相同的方式的服务:
Make the service in the same manner :
app.service('packageService', ['$rootScope','$http', function($rootScope, $http){
this.getPackageData=function(){
...
return...
};
}]);
请为第二个控制器不同的模块,并通过第一模块(控制面板)作为一个依赖:
Make a different module for second controller and pass First module (Dashboard) as a dependency :
var app2 = angular.module('SecondPagemodule', ['ngRoute', 'Dashboard']);
请的第2模块中的控制器,并通过该服务作为依赖和作为参数控制器的功能
Make the controller in the second module and pass the service as a dependency and as a parameter to the function of controller.
app2.controller('ControllerForSecondPage', ['$scope', '$http', 'PackageService', function($scope, $http, PackageService){
...
...
}]);
我希望这有助于。
I hope this helps.
这篇关于如何从服务与在angularjs响应数据控制器重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!