Passing the data of a successful promise to a variable as well to the $scope

380 views
Skip to first unread message

Constantinescu Nicolaie

unread,
May 1, 2014, 4:32:42 AM5/1/14
to ang...@googlegroups.com
Dear friends,

I have been learning to get around AngularJS in order to marry the data got from a MongDB (through PHP) to an OpenLayers 3 instance.

The main issue I have is that I do not know how to pass the data of a .success not only to the $scope, but also to a variable. This variable loaded in fact with a GeoJSON object (served by the PHP), will populate a vector layer of the OpenLayer's vector.

The problem is that I don't know how to pass the data to a variable.

THE FACTORY

(function(){

    var factGetFeat = function($http){    //folosesti serviciul angular $http
        
        var asyncData = {};
        
        asyncData.getWholeData = function(){
            return $http.get('./resources/allres.php');
        };

        return asyncData;
        
    };
    
    factGetFeat.$inject = ['$http'];

    angular.module('render')
           .factory('factGetFeat', factGetFeat);
}());

THE CONTROLLER

    var Mapper = function($scope, factGetFeat){

        $scope.allRO = {};

        factGetFeat.getWholeData() 
            .success(      
                function(data){
                    $scope.allRO = data;
//                    console.log(data);

                }
            )
            .error(
                function(data, status, headers, config){

                }
            );

// And here at the object I should need a variable loaded with my object

        var vectorSource = new ol.source.GeoJSON({
            object: theNeededVariable
        });

Thank you for your kind help.

Sander Elias

unread,
May 1, 2014, 5:11:23 AM5/1/14
to ang...@googlegroups.com

Hi,

Can you try something like this:

    var Mapper = function ($scope, factGetFeat) {
        var vectorSource;

        $scope.allRO = {};

        factGetFeat.getWholeData()
            .success(
                function (data) {
                    $scope.allRO = data;
                    vectorSource = new ol.source.GeoJSON({
                        object: data
                    });

                }
            )
            .error(
                function (data, status, headers, config) {
                }
        );
    };

This might just help…
Regards
Sander

Constantinescu Nicolaie

unread,
May 1, 2014, 9:02:09 AM5/1/14
to ang...@googlegroups.com
Interesting. I will try the minute I return to my computer. 

Any method to get the "data" obiect outside of the .success method scope!?
That would be a solution It would fir my needs better.

Thank you Elias, I will definetly try it.
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6h66_F6hKW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.


--
Constantinescu Nicolaie
Information Architect
http://www.kosson.ro
Membru în BEX al ANBPR
Director
Open Acces Understanding
Information Science Sonar
digital curator for Nostrum Monumentum

Sander Elias

unread,
May 1, 2014, 9:09:57 AM5/1/14
to ang...@googlegroups.com
Nicolaie,

Well, you can put an watch on the variable holding the data, but it is not available anyway before the data comes in from the back-end!

Regards
Sander

Constantinescu Nicolaie

unread,
May 1, 2014, 10:27:29 AM5/1/14
to ang...@googlegroups.com
The data is avalaible, I have checked with console.log(data) and the object is there. But is useless If I cannot use it outside .success method...


On Thursday, May 1, 2014, Sander Elias <sande...@gmail.com> wrote:
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6h66_F6hKW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
May 2, 2014, 1:54:01 AM5/2/14
to ang...@googlegroups.com

Hi Constantinescu,

By the time you can access your console, the data will be in. If you put an console.log in the code, you will see it’s not there yet.
If you really must do it outside the success method, you can do it like this:

$scope.$watch('allRO',function (newdata) {
    var vectorSource = new ol.source.GeoJSON({
         object: $scope.allRO
    }); 
});

This accomplish the same thing, but it takes some additional CPU cycles.

Regards
Sander

Constantinescu Nicolaie

unread,
May 2, 2014, 3:42:13 AM5/2/14
to ang...@googlegroups.com
I have tried to build the OpenLayers3 object inside success as you suggested.
Unfortunately it doesn't work. The data, hmmm, is very stubborn. It really sticks to being a promisse?!

By the time you chain a success, what is data by that time? Still a promise or the data representation?!

I will try the $watch...

If any of help I have packed the files. They are not much use without the MongoDB connection, but still.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6h66_F6hKW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
littleOL3app.zip

Constantinescu Nicolaie

unread,
May 2, 2014, 3:49:00 AM5/2/14
to ang...@googlegroups.com
I wonder why $scope.allRO is empty although I have a success. Ohhhh, I really don't want to get back to jQuery. Right, I will see if watching brings any success.

Sander Elias

unread,
May 2, 2014, 3:54:26 AM5/2/14
to ang...@googlegroups.com
The success method of the $http call will get the data, passed as data. In the attached sample, you don't put the data in $scope.allRO so you can watch endless, without getting anything!
perhaps you can do an console.log('data:',data) in your success method, and spy what is comming in?

Regards
Sander

Constantinescu Nicolaie

unread,
May 2, 2014, 4:13:33 AM5/2/14
to ang...@googlegroups.com


tested, all is there, but it seems $scope can offer data only to the view?! Damn data...


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6h66_F6hKW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Constantinescu Nicolaie

unread,
May 2, 2014, 5:48:05 AM5/2/14
to ang...@googlegroups.com

Sander Elias

unread,
May 2, 2014, 6:32:22 AM5/2/14
to ang...@googlegroups.com

Hi,

No, that’s not related. in that sample he is usings javascript’s this wrong.
And no again, $scope can deliver data anywhere you need it. However, it can not deliver data before it get’s it.

Let me sum up what is happening for you:

    function showMe($scope) {
        $scope.allRO = {} // Here I put in a reference to an empty object!

        $http('gogetStuff').success(function (data) {
            $scope.allRO = data;// Here I break the previous reference an put 
            // in a new one to data
        });

        somethingelse({
            dataIneed : $scope.allRO //put in a reference to currently empty object!
        });
    }

Keep in mind that the success is happening AFTER the somethingelse!. That means somethingelse will never get the data in this scenario

Regards
Sander

Constantinescu Nicolaie

unread,
May 2, 2014, 7:26:07 AM5/2/14
to ang...@googlegroups.com
Aha, that means the promise is fulfilled only when in $scope!?
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6h66_F6hKW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Witold Szczerba

unread,
May 2, 2014, 7:37:46 AM5/2/14
to ang...@googlegroups.com

Hi,
if you want my advice: stop writing your program and invest the time learning JavaScript and asynchronous execution flow. I mean it.

It's like you were learning how to write a bike by signing for a race. It won't work, trust me. You are crashing all the time making silly mistakes.

Take a JS course (I am sure there are free online courses), buy and read a books about programming, learn async stuff from ground up: callbacks, promises, etc...

When you are all ready you will be smiling to yourself reading the questions you ask here.

Regards,
Witold Szczerba

You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

Sander Elias

unread,
May 2, 2014, 7:58:17 AM5/2/14
to ang...@googlegroups.com
Hi Constantinescu,

I'm throwing in the towel. I think Witold has given you a solid recommendation!
Go learn about async and promises. If you still have issues after that, I will be glad to help you again.

Regards
Sander


Constantinescu Nicolaie

unread,
May 2, 2014, 8:58:45 AM5/2/14
to ang...@googlegroups.com
I'm on it! Thank you both!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6h66_F6hKW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages