angularjs - How to link Laravel controller function with Angular View -


i working on laravel web application , embedded angularjs searching data database. used array search.in angularjs module point search function $scope.url = 'students@search'; not working me. i want know how tell angularjs $scope.url point search function in controller user can search data easily. have search function in students controller:

 public function search(){             $data = file_get_contents("php://input");              $objdata = json_decode($data);              // static array demo             $values = array('php', 'web', 'angularjs', 'js');              // check if keywords in our array             if(in_array($objdata->data, $values)) {                 echo 'i have found you\'re looking for!';             }             else {                 echo 'sorry, no match!';             }   } 

myapp.js file :

function searchctrl($scope, $http) {     $scope.url = 'students@search'; // url of our search      // function executed on button click (ng-click="search()")     $scope.search = function() {          // create http post request         // data holds keywords         // request json request.         $http.post($scope.url, { "data" : $scope.keywords}).         success(function(data, status) {             $scope.status = status;             $scope.data = data;             $scope.result = data; // show result server in our <pre></pre> element         })         .         error(function(data, status) {             $scope.data = data || "request failed";             $scope.status = status;                  });     }; } 

angular search view:

<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 example: "php" or "angularjs" or "asdfg"</p>                                           </form>                                     <pre ng-model="result">                                     @{{result}}                                     </pre> </div> 

you may create route first. example-

route::get("/search", ["as" => "studentsearch", "uses" => "students@search"]); 

then can set like

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

or if not blade template have manually set url. like

 $scope.url = '/search'; 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -