javascript - How to redirect from the service to the controller with the response data in angularjs? -
i need , i'm new angularjs.
i have created app tours, here have 3 controllers each page , 1 service getting data response.
i have 4 button (links spanish, france, india, america, etc.) on each page footer.
my problem while clicking on location button in main controller (packagecontroller) i'm able access response data without redirecting because same controller, have other pages have controller 1 & 2, these controller need redirect main controller, i'm failing achieve this.
app:
var app = angular.module("dashboard");
service:
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; } } });
main controller: working
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; }); }; }
controller 1
if click on 1 of footer links site i'm calling gobtn()
, 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; }); }; }
controller 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; }); }; }
for page flow understaing, please see below image.
note: might approach wrong, please me!
try passing service dependency controllers, if doesn't work, try solving explained below :
i facing same problem. solved :
i made module different modules each pages, each module had controller in it. module of first page contained service. use same service in different controllers, passed first module (which contains service) dependency in other modules , passed service of first module dependency in controller , argument in function of controller.
so had separate javascript file each page module containing controller main module , service passed dependencies.
make first module :
var app = angular.module('dashboard', ['ngroute']);
make first controller in same manner.
make service in same manner :
app.service('packageservice', ['$rootscope','$http', function($rootscope, $http){ this.getpackagedata=function(){ ... return... }; }]);
make different module second controller , pass first module (dashboard) dependency :
var app2 = angular.module('secondpagemodule', ['ngroute', 'dashboard']);
make controller in second module , pass service dependency , parameter function of controller.
app2.controller('controllerforsecondpage', ['$scope', '$http', 'packageservice', function($scope, $http, packageservice){ ... ... }]);
i hope helps.
Comments
Post a Comment