0

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);

});
lipdjo
  • 161
  • 1
  • 4
  • 11

2 Answers2

2

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();
Community
  • 1
  • 1
Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
1

$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;
});
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156