2016-03-18 27 views
1

如何使用ngResource模塊如何從angularjs使用ngresouce

  1. 將數據發送到服務器將數據發送到服務器遙遠。從服務器
  2. 和獲取數據
  3. 我嘗試使用angularjs API
  4. 和NPM軟件包管理器安裝包到我的應用程序
  5. 感謝您的幫助,將數據發送到Solr服務器

controller.js:

var app=angular.module('myApp', ['ngResource']); 

app.controller('Mycontroller',function($scope, $resource) { 
    $scope.requete = $resource('http://localhost:8983/solr/#/cv/:action', 
     {action:'cv.json', q:'angularjs', callback:'JSON_CALLBACK'}, 
     {get:{method:'JSONP'}}); 

    $scope.chercher = function() { 
     $scope.repnses = $scope.requete.get({q:$scope.formInfo.Quoi}); 
    }; 
}) 

搜索html的:客戶

<!DOCTYPE html> 
<html ng-app="myApp"> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 

    </head> 
    <body > 
     <div ui-view></div> 
    <nav class="navbar navbar-default"> 
    <div class="container-fluid"> 
    <div class="navbar-header"> 
     <a class="navbar-brand" href="#">Formation</a> 
     <a class="navbar-brand" href="app/index.html">Formateur</a> 
    <a class="navbar-brand" href="app/search.html" active>Client</a> 
    </div> 
    </div> 
</nav> 
<div ng-contoller="Mycontroller"> 
<form class="form-horizontal" role="form"> 
<div class="table"> 
    <table class="table table-striped"> 
    <tr> 
     <td> 
      <label for="inputQuoi" class="col-sm-2 control-label">Quoi</label> 
     </td> 
     <td></td> 
    </tr> 
    <tr> 
     <td> 
     <input class="form-control" id="inputQuoi" placeholder="quoi" ng-model="formInfo.Quoi" required> 
      </td> 
     <td><button type="submit" ng-click="chercher()" class="btn btn-primary" >chercher</button></td> 
    </tr> 
    </table> 
    </div> 
</form> 
<table class="table table-striped"> 
     <tr ng-repeat="repnse in reponses.results"> 
      <td>{{requete.text}}</td> 
     </tr> 
    </table> 
</div> 

    <script src="app/js/controller.js"></script> 
    <script src="node_modules/angular-resource/angular-resource.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
    </body> 
</html> 

回答

0

不知道這是你的問題。 但是你有這個...

<tr ng-repeat="repnse in reponses.results"> 
     <td>{{requete.text}}</td> 
</tr> 

但真的沒有任何變量稱爲reponses.results,或requete作爲變量什麼。

您的代碼將獲取響應放在$ scope.repnses 因此,要循環訪問,您需要正確引用它。

<tr ng-repeat="repnse in repnses.results"> 
     // Then refer to the variables inside of it using the repnse variable. 
     // not requet, dont even know where you're getting that from 
     <td>{{repnse.text}}</td> 
</tr>