Hi R,
Perhaps you meant to use a service rather than a factory (i.e.
.service('faqlistService',[function(){ ...)
In your example, your factory doesn't return anything - i.e. it returns
undefined - which is why you are getting the undefined value in $scope.list
Here is a simple plnkr <http://plnkr.co/edit/89Sblk?p=preview> illustrating
the difference. Bottom line - a service is "new'd" when it is created, a
factory returns the object it creates.
Another good write up on the
differences:
http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory
Regards
Justin
On Thursday, October 9, 2014 3:46:00 AM UTC+2, [email protected] wrote:
>
> I'm new to AngularJS, so I'm sure I'm doing something wrong. ;-)
>
> I have this service:
>
> angular.module('TestApp')
>
> .factory('faqlistService',[function(){
> request = new XMLHttpRequest();
> request.open('GET', '
> http://www.humber.ca/elearning/BB9/sfq/js/faqlist.json', true);
>
> request.onload = function() {
> if (request.status >= 200 && request.status < 400){
> resp = request.responseText;
> console.log(resp);
> return resp;
> } else {
> // We reached our target server, but it returned an
> error
> console.log("error");
> }
> };
>
> request.onerror = function() {
> // There was a connection error of some sort
> console.log('connection error');
> };
>
> request.send();
> }]); // faqlistService {ends}
>
> it is used by this controller:
>
> .controller('NextCtrl',['$scope','$location','faqlistService',
> function($scope,$location,faqlistService){
> $scope.isVisible = true;
> $scope.next = "Next";
> $scope.loc = $location.url();
> console.log('Ask faqlist');
> $scope.list = faqlistService;
> console.warn($scope.list)
> }])
>
>
> The result is that $scopellist is undefined but I would expect to have my
> json result. What am I doing wrong?
> BTW. I used $http too and I get the same result.
>
> Any help would be appreciated.
>
> R.
>
--
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.