V
V
Vladislav Kalmykov2016-11-16 10:29:31
Angular
Vladislav Kalmykov, 2016-11-16 10:29:31

Why can't I update data when receiving an event?

Hello!
Please tell me the problem.
There is a function that adds data to the database.
After adding, the $rootScope.$broadcast('db:change') event is dispatched:

$scope.addDb = function (name) {
        NetFactory.addDb(name).then(
            function (data) {
                $scope.message = data;
            }
        );

        $rootScope.$broadcast('db:change');
};

Code that listens to the event:
$rootScope.$on('db:change', function () {
        NetFactory.getDbList().then(function (value) {
            $scope.dbList = value;
        });
});

When I click on the button that calls the add function, the data is updated in different ways - it will be updated immediately, then it will be updated after the second addition or the third, etc.
Tell me, please, how to solve the problem?
Or is it possible to somehow better organize data updating, including in different sibling controllers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Drapeza, 2016-11-16
@Davert94

Good afternoon!
Broadcast is called before the data is successfully added, so it should be moved to then. Try like this:

$scope.addDb = function (name) {
        NetFactory.addDb(name).then(
            function (data) {
                $scope.message = data;
                $rootScope.$broadcast('db:change');
            }
        );
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question