0

i'm a biginner when it comes to Angular.js, and i have a problem with $scope not getting additional value from one of two $resource rest calls. Here's my code:

controller: function ($scope, $modalInstance, $route) {
            $scope.server = {}

            $scope.submit = function () {
                //AddNewServer is a $resource service
                AddNewServer.post($.param({
                    'name': $scope.server.name,
                    'ip': $scope.server.ip,
                    'port': $scope.server.port
                }));

                //ServerStats is a $resource service
                ServerStats.postServerStats(function success(data) {
                    $scope.server.bytesIn = data.returnValue.bytesIn
                }, function err(err) {
                    console.log("Error: " + err)
                })

                $modalInstance.dismiss('cancel');
                $route.reload()

                //BELLOW LOG RETURNS Object {name: "asd", ip: "asd", port: 2} NO bytesIn
                console.log($scope.server) 
            }

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
                $route.reload()
            };
        }

Question is how do i add bytesIn from my other service call into my server object? I'm sure it a pretty obvious thing but i'm still in learning phase. Thanks in advance.

1
  • 1
    don't forget to mark an answer as correct if it helped you! Commented Dec 12, 2013 at 14:53

1 Answer 1

1

Your postServerStats() call is asynchronous, so it's likely that your success function isn't being called before the console.log($scope.server) statement.

Put console.log($scope.server) in your success function, after you assign $scope.server.bytesIn.

Perhaps you mean to do more work in your postServerStats() callback?

Or better yet, look into angular promises

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply! I added console.log($scope.server) in my success callback and it is giving me object that i want: {name: "asd", ip: "asd", port: 2, bytesIn: 7076}. Whan i want to do is to be able to use that $scope.server on my page to show data, but i get only first 3 parameters (name, ip, port) and not bytesIn if i use it in lets say div with ng-repeat.

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.