简体   繁体   English

如何使用angularjs中的响应数据从服务重定向到控制器?

[英]How to redirect from the service to the controller with the response data in angularjs?

I need some help and I'm new to the angularjs. 我需要一些帮助,所以我是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. 我在每个页面页脚上都有四个按钮(链接看起来像西班牙语,法国,印度,美国等)。

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. 我的问题是,单击主控制器(packageController)中的位置按钮时,我能够访问响应数据而无需重定向,因为这是同一控制器,但是我还有其他页面包含控制器1和2,我需要这些控制器重定向到主控制器,但是我无法实现这一点。

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: This is 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 控制器1

If i click on one of those footer links from my site i'm calling goBtn() , but is not redirecting. 如果我从网站上单击这些页脚链接之一,则表示正在调用goBtn() ,但未进行重定向。

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 控制器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 this below image. 对于页面流不足,请参见下图。

在此处输入图片说明

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. 为了在不同的控制器中使用相同的服务,我将第一个模块(包含该服务)作为其他模块中的依赖项传递,然后将第一个模块的服务作为控制器中的依赖项以及参数的传递函数。控制器。

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. 因此,基本上每个页面都有一个单独的Javascript文件,每个模块都有一个包含控制器的模块,该模块具有作为依赖项传递的主模块和服务。

Make the first module : 制作第一个模块:

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']);

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. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM