2016-04-25 10 views
1

私は最近、データベースからデータを検索するためのanglejsを埋め込んだlaravel Webアプリケーションに取り組んでいます。しかし、今のところ私はちょうど検索配列に使用しました。私のAngularJsモジュールでは、私は$scope.url = '[email protected]';として検索機能を指していますが、これは私のために働いていません。 AngularJS $scope.urlに私のコントローラのこの検索機能を指し示す方法を知りたいので、ユーザーが簡単にデータを検索できるようにしたい。 私は学生コントローラの検索機能を持っている:Laravelコントローラの機能を角度表示にリンクするには

public function search(){ 
      $data = file_get_contents("php://input"); 

      $objData = json_decode($data); 

      // Static array for this demo 
      $values = array('php', 'web', 'angularjs', 'js'); 

      // Check if the keywords are in our array 
      if(in_array($objData->data, $values)) { 
       echo 'I have found what you\'re looking for!'; 
      } 
      else { 
       echo 'Sorry, no match!'; 
      } 
    } 

MyApp.jsファイル:

function SearchCtrl($scope, $http) { 
    $scope.url = '[email protected]'; // The url of our search 

    // The function that will be executed on button click (ng-click="search()") 
    $scope.search = function() { 

     // Create the http post request 
     // the data holds the keywords 
     // The request is a JSON request. 
     $http.post($scope.url, { "data" : $scope.keywords}). 
     success(function(data, status) { 
      $scope.status = status; 
      $scope.data = data; 
      $scope.result = data; // Show result from server in our <pre></pre> element 
     }) 
     . 
     error(function(data, status) { 
      $scope.data = data || "Request failed"; 
      $scope.status = status;   
     }); 
    }; 
} 

アンギュラ検索ビュー:

<div ng-controller="SearchCtrl"> 
            <form class="well form-search"> 
             <label>Search:</label> 
             <input type="text" ng-model="keywords" class="input-medium search-query" placeholder="Keywords..."> 
             <button type="submit" class="btn" ng-click="search()">Search</button> 
             <p class="help-block">Try for example: "php" or "angularjs" or "asdfg"</p>  
            </form> 
            <pre ng-model="result"> 
            @{{result}} 
            </pre> 
</div> 

答えて

0

あなたが最初のルートを作成することができます。例 -

Route::get("/search", ["as" => "studentSearch", "uses" => "[email protected]"]); 

のために、あなたは、あなたが手動でURLを設定する必要が

$scope.url = '{{ route ("studentSearch") }}'; 

またはそのない場合は、ブレードのテンプレートのようにそれを設定することができます。同様に

$scope.url = '/search'; 
+0

@Shimul –

+0

Route [studentSearch]が定義されていません。 –

+0

私の編集をチェックしてください。ルートはRoute :: get( "/ search"、 "as" = "" studentSearch ""、 "uses" => "Students @ search"])になります。 –

関連する問題