Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been trying to get user data from a rails controller to an angular controller:

In my application.html.erb, I define User like so:

angular.element(document).ready(function() {
    angular.module('myModule').value('User', <%= @user.to_json.html_safe %>);
});

In my angular controller:

myModule.controller('MyController', ['$scope', 'User', function($scope, User) {
    $scope.user = User;
}]);

The problem with this is that it exposes the user data to the view. Is there a better way to do this?

I've also tried this approach:

In my module, I created a factory with a $resource object:

myModule.factory('User', function($resource) {
    return $resource('/users/:action', {},
    {
        'getUser': { method: 'GET', params: { action: 'fetch_user.json' }, isArray:true }
    });    
});

In my controller:

myModule.controller('MyController', ['$scope', 'User', function($scope, User) {
    $scope.user = User.getUser();

    // Also tried the following.
    // This will throw undefined errors in places that are trying to access $scope.user
    User.getUser({}, function(data) {
        $scope.user = data;
    });
}]);

However, this either does not load the user data on time and is throwing undefined errors throughout my application or it will put every character of the result in it's own array container.

The @user variable is being returned as json from the server in the users_controller.rb so ideally, I would want to retrieve that and store it in $scope.user. The problem with this is that it requires the username to be passed as a parameter to retrieve the correct user's data.

How can I set $scope.user correctly and cleanly without exposing data?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

Directive

myModule.factory('User', function($http) {
    return {
        getUser: function () {
            // deferred object
            var deferred = $q.defer();
            $http.get('/fetch_user.json') {
                .success(function (data, status) {
                     deferred.resolve(data);
                })
                .error(function (data, status) {
                     deferred.reject("An error occured while trying to get users");
                });
            }
            return deferred.promise;
       }
    }
}

Contoller:

myModule.controller('MyController', ['$scope', 'User', function($scope, User) {
     User.getUser().then(function (data) {
         $scope.user = data;
     });
}]);

I'm assuming /fetch_user.json is the url, if not just replace it.

Update

share|improve this answer
I don't think the syntax is correct. – rabid_zombie 10 hours ago
Which part of the code? – dcodesmith 10 hours ago
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.