2016-06-14 2 views
0

Ich bin neu bei angularjs, ich versuche einen $ http.post Aufruf zu machen. Hier ist mein HTML-Code:

<div class="simple-controller" ng-controller="Simple.SimpleController"> 

    <button type="button" id="create" name="create" data-toggle="modal" 
     data-target="#myModal" style="margin-left: 45%; margin-bottom: 1%; margin-top: 2%">CREATE</button> 
    <div class="modal fade" id="myModal" role="dialog"> 
     <div class="modal-dialog"> 

      <!-- Modal content--> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal">&times;</button> 
        <h4 class="modal-title">Add Details</h4> 
       </div> 
       <div class="modal-body"> 
        <div class="form-group"> 
         <label for="usr">Name:</label> <input type="text" 
          class="form-control" id="usr"> 
        </div> 
        <div class="form-group"> 
         <label for="pwd">Service:</label> <input type="text" 
          class="form-control" id="service"> 
        </div> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" onclick="addData()" class="btn btn-primary">Save</button> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

Ich versuche, einen Beitrag Anruf zu einem Rest-Dienst zu machen lokal mit Namen und Service Eingabetyp als Pfad params. So sollte meine URL wie folgt aussehen http://localhost:8181/cxf/authorization/addData/Siddhu/jhg

Hier ist mein js Code für die Herstellung der Post Aufruf:

function addData($scope, $http) { 
    var name = document.getElementById("usr").value; 
    var service = document.getElementById("service").value; 
    var url = 'http://localhost:8181/cxf/authorization/addData'; 
    alert('URL ==== '+url); 
    $http.post(url,{name:name, service:service}) 
    .success(function(data) { 
     $scope.data = data; 
     alert('THE DATA IS === '+$scope.data); 
    }); 
} 

I Uncaught TypeError: Cannot read property 'post' of undefined error bekommen.

Kann mir jemand den richtigen Weg zeigen, den Anruf zu tätigen?

Antwort

0

So löste ich das Problem für diejenigen, die den Code suchen. Statt Onclick Ich habe ng-Click-Richtlinie:

<button type="button" ng-click="addData()" class="btn btn-primary">Save</button> 

Und den Code für meine Funktion:

$scope.addData = function() { 
     var name = $scope.usr; 
     var service = $scope.service; 
     var url = 'http://localhost:8181/cxf/authorization/addData/'+name+'/'+service; 
     $http.post(url) 
     .success(function(data) { 
      if(data == 'Updated'){ 
       fetchData(); 
       $('#myModal').modal("hide"); 
      } 
      else{ 
       alert("Details not added"); 
       $('#myModal').modal("hide"); 
      } 


     }); 
    } 
2

Ihre addData Funktion erfordert zwei Parameter ($ Umfang und $ http), aber wenn es aus dem HTML genannt wird man auch nicht in.

Ich vermute, Sie verdrahtet in dem $ HTTP-Dienst in Ihren Controller ist vorbei, aber dann ersetzen Sie das mit dem Methodenparameter.

+0

Das ist richtig! Können Sie uns Ihren gesamten Winkelregler zeigen? Etwas stimmt nicht mit deiner Abhängigkeitsinjektion. – brammekuhh

+0

Hallo, du hattest recht, ich brauchte ng-click. Problem gelöst! :) –

+0

@brammekuhh Ich habe den Arbeitscode unten veröffentlicht. –

Verwandte Themen