2014-10-03 6 views
1

이것은 매우 기본적인 질문 일 수 있지만 답변을 찾을 수 없습니다. 방금 AngularJs를 배우기 시작했고 컨트롤러에 간단한 페이지를 작성하려고합니다. 여기서는 입력에 텍스트를 입력하고 HTTP GET 요청을 할 수 있습니다. 지금까지 나는 다음이 : 나는 $에 대한 참조가 없기 때문에 라인 this.searchRequest($http, url); 분명히 잘못AngularJS 컨트롤러 메서드에 매개 변수 전달

angular.module('starter', []) 
    .controller('searchController', function() { 
    this.searchURL = 'http://www.myapifilms.com/imdb?title=%thetitle%&format=JSON'; 
    this.searchString = ''; 

    this.search = function searchMovie() { 
     url = this.searchURL.replace('%thetitle%', this.searchString); 
     console.log("URL: " + url); 
     this.searchRequest($http, url); 
    }; 

    this.searchRequest = function ($http, url) { 
     $http.get(url).success(function(data) { 
      console.log("Success: " + data); 
     }) 
    }; 
}); 

컨트롤러에서 HTML에서

<body ng-app="starter" ng-controller="searchController as searchController"> 

<ion-pane> 
    <ion-header-bar class="bar-stable"> 
    <h1 class="title">Ionic Blank Starter</h1> 
    </ion-header-bar> 
    <ion-content> 
    <input type="text" ng-model="searchController.searchString" required /> 
    <button class="btn" ng-click="searchController.search()">Search</button> 
    </ion-content> 
</ion-pane> 

HTTP, AngularJs에 의해 나중에 주입 될 것입니다.

내 질문은 내 자신의 전달 된 매개 변수와 $ http와 같은 AngularJs 서비스를 어떻게 혼합합니까? 어쩌면 나는 이것을 적절하게 구조화하지 않고 그것에 대한 의견을 듣고 싶어한다. 내 목표는 단지 ​​입력 텍스트를 가져 와서 HTTP 요청을하는 것이다.

답변

2

당신은 방법, 감사 속임수를 썼는지

angular.module('starter', []) 
    .controller('searchController', ["$http", function($http) { 
     ... 
    }]); 
+0

에 전달하는 대신 컨트롤러 선언 서비스 (각도 또는 사용자 정의 서비스)를 주입한다! – momo

관련 문제