1

I am new to angular and I still following tutorials and stuff to figure things out. I need some help with the following. I have a controller that looks like this.

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

}]);

A controller which calls an $http service to get the list of booking and in HTML I am populating the response with ng-repeat.

I have another controller like this.

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

}]);

So scenario is: When the page load customer should see all the booking he has been made. When he make booking a http service is called to make booking and in this service success call back I want to do something so that it can refresh the list of booking by calling http service defined in Schedule booking controller. May be I can do it with BROADCAST and ON method. But I am not sure. There are a lot of similar things happening in my already JQuery written application. What is the BEST way to do this? May be I am taking this totally wrong and there is any other better way to do it. What do you guys suggest?

Martin
  • 2,411
  • 11
  • 28
  • 30
Burhan Noman
  • 13
  • 1
  • 3

2 Answers2

1

Since ScheduleBooking seems not doing much more than call endpoint the best way is to turn it into service and inject this service in each controller you need to call particular method (or get data from it), something like this:

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

}]);
maque
  • 676
  • 1
  • 6
  • 11
  • Thank you for answering question. The problem is MakeBooking controller doesn't depend upon the data of schedule booking. I don't want to operate schedule booking controller's data in makebooking controller. I just want to alert schedule booking controller from makebooking controller to run once (so that it can update **schedule.databooking** array and DOM gets updated.) Getting my point? – Burhan Noman Apr 20 '15 at 04:50
  • Well in that case all depends how you structure your controllers on page. If they are in parent->child relationship then you can try invoke it with $scope.$parent if not then yes, broadcasting seems to be best way, yet still I believie you can avoid this and it's better to inject services (or if it has to be controller you can inject $controller ans us it as described http://stackoverflow.com/questions/25417162/how-do-i-inject-a-controller-into-another-controller-in-angularjs – maque Apr 20 '15 at 06:05
0

@kmdsax answer solved my problem. Adding a watch in controller 2 works!

Check Kmdsax answer on this post.

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

Community
  • 1
  • 1
Burhan Noman
  • 13
  • 1
  • 3