Hello!

I try to implement a very simple GET function to fetch data from Asp.Net 
controller, which is:

    public class UserController : ApiController
    {
        // GET: api/User
        public IEnumerable<string> Get()
        {
            return new[] { "value1", "value2"  };
        }

        // GET: api/User/5
        public string Get(int id)
        {
            return "value";
        }
    }

On the client I use HotTowel and SideWaffle templates creating two variant 
of factories.

1 - resource variant:
(function () {
    'use strict';

    var serviceId = 'userfactory';

    angular.module('app').factory(serviceId, ['$resource', userfactory]);

    function userfactory($resource) {
        // Define the functions and properties to reveal.
        var service = {
            getData: getData
        };

        return service;

        function getData() {
            return $resource('/api/user/5').query();
        }
    }
})();

2 - http variant
(function () {
    'use strict';

    var serviceId = 'userfactoryhttp';

    angular.module('app').factory(serviceId, ['$http', '$q', 
userfactoryhttp]);

    function userfactoryhttp($http, $q) {
        // Define the functions and properties to reveal.
        var service = {
            getData: getData
        };

        return service;

        function getData() {
            var deferred = $q.defer();
            
$http.get('/api/user/').success(deferred.resolve).error(deferred.reject);
            return deferred.promise;
        }
    }
})();

And finally page controller with both factories injected:
(function () {
    'use strict';
    var controllerId = 'userprofile';
    angular.module('app').controller(controllerId, ['common', 
'userfactoryhttp', 'userfactory', userprofile]);

    function userprofile(common, userfactory, userfactoryhttp) {
    var getLogFn = common.logger.getLogFn;
    var log = getLogFn(controllerId);

    var vm = this;

    vm.title = 'UserProfile';
    vm.user = [];

    activate();

    function activate() {
        var promises = [getUserHttp(), getUser()];
        common.activateController(promises, controllerId)
            .then(function () { log('Activated UserProfile View'); });
    }

    function getUser() {
        vm.user = userfactory.getData();
    }

    function getUserHttp() {
        return userfactoryhttp.getData.then(function (data, status, 
headers, config) {
            return vm.userhttp = status;
        });
    }
}
})();

Why do I have a error refreshing a page?
TypeError: Object function getData() { return 
$resource('/api/user/5').query(); } has no method 'then'

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to