Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling order of execution in angularjs

Tags:

angularjs

I have inherited an angular app and now need to make a change.

As part of this change, some data needs to be set in one controller and then used from another. So I created a service and had one controller write data into it and one controller read data out of it.

angular.module('appRoot.controllers')

.controller('pageController', function (myApiService, myService) {
    // load data from API call
    var data = myApiService.getData();

    // Write data into service
    myService.addData(data);
})

.controller('pageSubController', function (myService) {
    // Read data from service
    var data = myService.getData();

    // Do something with data....
})

However, when I go to use data in pageSubController it is always undefined.

How can I make sure that pageController executes before pageSubController? Or is that even the right question to ask?

EDIT

My service code:

angular.module('appRoot.factories')

.factory('myService', function () {
    var data = [];

    var addData = function (d) {
        data = d;
    };

    var getData = function () {
        return data;
    };

    return {
        addData: addData,
        getData: getData
    };
})
like image 766
tom redfern Avatar asked Nov 06 '14 11:11

tom redfern


People also ask

What is a controller AngularJS?

The controller in AngularJS is a JavaScript function that maintains the application data and behavior using $scope object. You can attach properties and methods to the $scope object inside a controller function, which in turn will add/update the data and attach behaviours to HTML elements.

What is attrs in AngularJS?

Using attrs you are able to access the attributes defined in your html tag like <fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true"> So in this case you will have access to the symbol and readonly attributes.

Who controls the data flow in AngularJS?

AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is defined using ng-controller directive. A controller is a JavaScript object that contains attributes/properties, and functions.

How directive works in AngularJS?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.


1 Answers

If you want your controller to wait untill you get a response from the other controller. You can try using $broadcast option in angularjs.

In the pagecontroller, you have to broadcast your message "dataAdded" and in the pagesubcontroller you have to wait for the message using $scope.$on and then process "getData" function.

You can try something like this :

angular.module('appRoot.controllers')
.controller('pageController', function (myApiService, myService,$rootScope) {
    // load data from API call
    var data = myApiService.getData();

    // Write data into service
    myService.addData(data);
    $rootScope.$broadcast('dataAdded', data);
})

.controller('pageSubController', function (myService,$rootScope) {
    // Read data from service
    $scope.$on('dataAdded', function(event, data) {
        var data = myService.getData();
    }

    // Do something with data....
})
like image 144
Sangeetha Pelluri Avatar answered Oct 17 '22 04:10

Sangeetha Pelluri