1

If I reload the data according to my function, the view does't change. I found, that should be solved by adding $scope.$apply(). Somewhere after updating the scope, but in this case i am getting this error.

https://docs.angularjs.org/error/$rootScope/inprog?p0=$apply

My script:

var roomsApp = angular.module('roomsApp', []);
roomsApp.controller('RoomListController', function RoomListController($scope, $http) {
    $scope.refreshData = function () {
        $http({
            method: 'GET',
            url: '<?=url('getRooms')?>'
        }).then(function successCallback(response) {
            $scope.rooms = response.data;
            console.log(response);
        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    }
    $scope.refreshData();

    $scope.addRoom = function (title) {
        if (title != "" && typeof title !== 'undefined') {
            $http({
                method: 'POST',
                url: '<?=url('addRoom')?>',
                data: {
                    title: title
                }
            }).then(function successCallback(response) {
                $scope.refreshData();
            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
            });
        }
        $scope.$apply();
    };

});

My angular - HTML:

<div class="row" ng-app="roomsApp">
    <div class="col-md-8">
        <div class="panel panel-default" ng-controller="RoomListController">
            <div class="panel-heading">Rooms</div>
            <div class="panel-body">
                <ul class='list-group' ng-repeat="room in rooms">
                    <li class="list-group-item">{{room.title}}</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="panel panel-default" ng-controller="RoomListController">
            <div class="panel-heading">New room</div>
            <div class="panel-body">
                <div class="input-group input-group-lg">
                    <span class="input-group-addon" id="title">Title</span>
                    <input type="text" ng-model="title" class="form-control" placeholder="" aria-describedby="title">
                </div>
                <br>
                <button ng-click="addRoom(title);" type="button" class="btn btn-primary pull-right"><span
                        class="glyphicon glyphicon-plus"></span></button>
            </div>
        </div>
    </div>
</div>
5
  • Does your post method work? Commented Jun 8, 2016 at 12:15
  • What is <?=url('getRooms')?>? Commented Jun 8, 2016 at 12:15
  • Looks like some php ^^ Commented Jun 8, 2016 at 12:23
  • $scope.$apply(); should be in your callback...if its needed. You probably are still fetching data as the digest phase is in process. Commented Jun 8, 2016 at 12:53
  • The post method works properly, if i console.log it, it returns the new data. Commented Jun 8, 2016 at 13:21

2 Answers 2

1

Try this one:

$scope.$apply(function(){
    $scope.rooms = response.data;   
});
Sign up to request clarification or add additional context in comments.

1 Comment

This code should be also called inside of then function, because promises are asynchronous. If you put is just at the end of addRoom then $scope.$apply() will be called before request finishes.
0
  1. Remove $scope.$apply();
  2. From your HTML, remove both ng-controller="RoomListController" attributes
  3. ng-controller="RoomListController" on <div class="row" ng-app="roomsApp">

Reason: Right now, your current code creates two separate scopes of RoomListControllers. Problem is, you are expecting to update the list on the first controller scope whilst executing the addRoom method on the second controller scope. The two controllers can't communicate with each other.

p.s. if you need to have two difference controller scopes, consider using rootScope.broadcast - rootScope.on pattern to faciliate communication between the two sibling scopes

1 Comment

Thanks a lot. I copied the bootstrap panel and forgot to remove the ng-conroller tag. So the solution has nothing to do with $scope.$apply(); It runs without that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.