2014-11-30 2 views
1

$ http와 내 맞춤 서비스를 사용할 때 TypeError: Cannot read property 'success' of undefined 오류가 발생했습니다.

내 app.js :

app.controller('AppCtrl', ['$scope', '$http', 'topicContent', function($scope, $http, topicContent){ 

    topicContent.request().success(function(data){ 
    $scope.threadContent = data; 
    console.log(data); 
    }); 

}]); 

app.factory('topicContent', ['$http', function($http){ 

    var query = function() { 
     return 
      $http({ 
      url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php", 
      method: "GET" 
     }); 
    } 

    return { 
     request : function(){ 
      return query(); 
     } 


    } 
}]); 

이 너무 이상하다, 나는 그것의 어떤 결함을 찾을 수 없습니다.

답변

0

문제는 코드

var query = function() { 
    return 
     $http({ 
     url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php", 
     method: "GET" 
    }); 
} 

은 이것에 동일한 점이다

var query = function() { 
    return; 
     $http({ 
     url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php", 
     method: "GET" 
    }); 
} 

때문에 스크립트 자동 세미콜론 삽입한다. return 다음에는 세미콜론 ;이 있습니다. 그래서 해결책은 return 문 다음에 같은 줄에 $http을 이동하는 것입니다 : 통역 삽입 예를 here를 들어, 자동으로 ;을 세미콜론 자바 스크립트 어떤 상황에 대한

return $http({ ... }); 

읽기. 귀하의 경우에 return 통역사 ; 삽입 query 함수를 undefined 반환하게됩니다 후. 따라서 오류가 발생합니다.

+0

아, 정말 고마워요! 오늘 뭔가를 배웠다! –

관련 문제