read JSON file from a factory

2,286 views
Skip to first unread message

Beat Ottiger

unread,
Apr 10, 2013, 3:50:57 PM4/10/13
to ang...@googlegroups.com


Hi!

I am totally new to angular - I am struggling with these simple tasks:

  1. Read a Server JSON File (get_current_user.php):

    Looks like:
    {"id":1,"user_name":"Admin","display_name":"Admin","password":"very_encrypted","email":"ga...@gaga.org","activation_token":"very_encrypted","last_activation_request":1365591749,"lost_password_request":0,"active":1,"title":"New Member","sign_up_stamp":1365591749,"last_sign_in_stamp":1365618467}


  2. Create a 'factory' to read the above JSON.
    The idea is that i can later 'inject' the factory in different Controllers - so I can read the current user everywhere.
    factory should return a current user object

  3. Incect the 'factory' in my Controllers:
    like:
    function PhoneListCtrl($scope, $http, Benutzer) {
        $scope.benutzer = Benutzer;
    ....

Sounds easy for you?  Thanks for pointing me in the right direction!


Something like the following code - does not work at all....  
...what is wrong?


app.factory('Benutzer', function ($http) {
    return {
        //Benutzername: "Data von meinem Service!"
        $http.get('get_current_user.php').success(function (data) {
            this.myBenutzer = data;
        });
});


Many thanks!

Greetings from switzerland!
Beat

yahya Kacem

unread,
Apr 10, 2013, 5:22:52 PM4/10/13
to ang...@googlegroups.com
Try this plunker.
Hope this help.

Beat Ottiger

unread,
Apr 10, 2013, 5:39:49 PM4/10/13
to ang...@googlegroups.com

Perfect! Many thanks!  

Beat Ottiger

unread,
Apr 11, 2013, 3:43:50 AM4/11/13
to ang...@googlegroups.com


Hi Kacem

Can't make it work.... sorry - had to ask again:

When I console.log($scope); in MainCtrl 'user' looks like:

user: "Array?(?    [id] => 1?    [user_name] => Admin?    [display_name] => Admin?    [password] => very_encrypted?    [.....


In your plunker user is an Object - here it is a kind of Array.  What am I doning wrong?  Any Idea?



My Ctrl looks like:
function PhoneListCtrl($scope, $http, User) {

    User.then(function(data){
        //console.log('Userdata vorhanden.');
        $scope.user = data;
        //console.log($scope.user);
        //console.log(data.display_name);
    });
.....
}
PhoneListCtrl.$inject = ['$scope', '$http', 'User'];

My app.factory looks like:  (Placed in app.js - should not be a problem - or?)
app.factory('User', ['$http', function($http){
    var Url  = "get_current_user.php";
    var User = $http.get(Url).then(function(response){
        //console.log(response.data);
        return response.data;
    });

    return User;
}]);




Many THANKS!




Am Mittwoch, 10. April 2013 23:22:52 UTC+2 schrieb yahya Kacem:

yahya Kacem

unread,
Apr 11, 2013, 3:49:54 AM4/11/13
to ang...@googlegroups.com
Did you json_encode your code in the PHP script?
And if not try to make an example plunker that i can modify that would be easier to understand.


On Wednesday, April 10, 2013 8:50:57 PM UTC+1, Beat Ottiger wrote:

Beat Ottiger

unread,
Apr 11, 2013, 3:58:14 AM4/11/13
to ang...@googlegroups.com

....it is defenitly a PHP problem. When I make a txt file and put my JSON string in - your code is working perfect!
    Oh wow....   had to check my PHP!   :-)

There is no Thanks Button or something in Google Groups?  I would hit it 10x!  

Wish you a good day!
Beat



Mit freundlichen Grüssen
Beat Ottiger

_________________________________

  Beat Ottiger
  Alte Landstrasse 31, CH - 8800 Thalwil

:: Tel. +41 (0)79 635 85 84
:: www.ottiger.org | otti...@gmail.com
_________________________________



2013/4/11 yahya Kacem <fuj....@gmail.com>

--
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/jp6qOuH-KaM/unsubscribe?hl=en-US.
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?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages