lipdjo
lipdjo

Reputation: 161

What's wrong with this service in angularjs?

Please I have two questions , For huge applications, Angularjs , Is it a good solution in term of cpu usage?

What is wrong with my angular code?

var myapp = angular.module('myapp', []);

myapp.service('server', function (url){
    this.get = function ($http){
    return $http.get(url);
    };        
});



myapp.controller('myctrl' , function($scope,server){

    setInterval(function(){

        $scope.r= server.get('response.js');

    },1000);

});

Upvotes: 0

Views: 51

Answers (2)

Benny Bottema
Benny Bottema

Reputation: 11493

You misplaced your $http dependency:

myapp.service('server', function (url){
    this.get = function ($http){
        return $http.get(url);
    };        
});

Should be

myapp.service('server', function ($http){
    this.get = function (url){
        return $http.get(url);
    };        
});

Also, you are using the return value of $http.get() as if you are donig a $resource.get(). This won't work for array results. Consider using $resource instead:

return $resource(url).get();

Upvotes: 2

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38478

$http.get returns a promise. You will have use a callback to get the result:

server.get('response.js').success(function(data){
    $scope.r = data;
});

Upvotes: 1

Related Questions