【问题标题】:How to call controller's service from another controller success call back in Angular JS如何从Angular JS中的另一个控制器成功回调调用控制器服务
【发布时间】:2015-06-26 09:02:33
【问题描述】:

我是 Angular 的新手,我仍然按照教程和其他东西来解决问题。我需要以下帮助。 我有一个看起来像这样的控制器。

app.controller('ScheduleBooking', ['$http', function($http){
    var schedule = this;
    schedule.databooking = [];

    $http.post('../getbookings.json').
        success(function(data, status, headers, config) {
            schedule.databooking = data;
        }).
        error(function(data, status, headers, config) {
            console.log('failed');
        });

}]);

一个控制器,它调用 $http 服务来获取预订列表,我在 HTML 中使用 ng-repeat 填充响应。

我有另一个这样的控制器。

app.controller('MakeBooking', ['$http', function($http){
    var makeBooking = this;
//somecode here

    $http.post('../MakeBooking.json?name=burhan').
        success(function(data, status, headers, config) {
            // I WANT TO REFRESH THE LIST OF BOOKINGS.
//I am looking a way to call scheduleBooking controller so that 
//it can make http call and refresh the list of booking in html.
        }).
        error(function(data, status, headers, config) {
            console.log('failed');
        });

}]);

所以场景是:当页面加载时,客户应该看到他所做的所有预订。当他进行预订时,会调用 http 服务进行预订,并且在此服务成功回调中,我想做一些事情,以便它可以通过调用 Schedule booking 控制器中定义的 http 服务来刷新预订列表。 也许我可以用 BROADCAST 和 ON 方法做到这一点。但我不确定。在我已经用 JQuery 编写的应用程序中发生了很多类似的事情。 做这个的最好方式是什么?可能是我完全错了,还有其他更好的方法可以做到这一点。 你们有什么建议?

【问题讨论】:

    标签: angularjs http callback controller angularjs-service


    【解决方案1】:

    由于 ScheduleBooking 似乎除了调用端点之外没有做更多的事情,最好的方法是将其转换为服务并将此服务注入您需要调用特定方法(或从中获取数据)的每个控制器中,如下所示:

    app.factory('ScheduleBookingSerice', ['$http', function($http){
    
        var schedule = this;
    
        schedule.getBookings = function(callback){$http.post('../getbookings.json').
            success(function(data, status, headers, config) {
                callback(data);
            }).
            error(function(data, status, headers, config) {
                console.log('failed');
            });
        }   
        return schedule;
    }]);
    
    
    app.controller('MakeBooking', ['$http', 'ScheduleBookingSerice', function($http, ScheduleBookingSerice){
        var makeBooking = this;
    //somecode here
    
        $http.post('../MakeBooking.json?name=burhan').
            success(function(data, status, headers, config) {
                ScheduleBookingSerice.getBookings(function success(data){
                //operate on your data here
                });
            }).
            error(function(data, status, headers, config) {
                console.log('failed');
            });
    
    }]);
    

    【讨论】:

    • 感谢您回答问题。问题是 MakeBooking 控制器不依赖于计划预订的数据。我不想在 makebooking 控制器中操作计划预订控制器的数据。我只想从 makebooking 控制器提醒调度预订控制器运行一次(以便它可以更新 schedule.databooking 数组并更新 DOM。)明白我的意思了吗?
    • 那么在这种情况下,一切都取决于您如何在页面上构建控制器。如果它们处于父->子关系,那么您可以尝试使用 $scope.$parent 调用它,如果不是,那么是的,广播似乎是最好的方式,但我仍然相信您可以避免这种情况,最好注入服务(或者如果它必须是控制器,您可以按照stackoverflow.com/questions/25417162/… 的描述注入 $controller 和我们
    【解决方案2】:

    @kmdsax 回答解决了我的问题。在控制器 2 中添加手表有效!

    在这篇文章中查看 Kmdsax 的答案。

    How can I pass some data from one controller to another peer controller

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-13
      • 2014-10-23
      • 1970-01-01
      • 2013-07-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多