How to get value from a form with AngularJS?

0

I'm starting with AngularJS and there's a question that seems to me to be simple.

I'm doing a shopping cart (just for the same apprenticeship) with what I learned (ng-controller, ng-repeat, ng-model, etc.).

I have a list that comes from the Controller and I want to automatically add in the affection. However, I would like to know how I get the value of the quantity and step for the function in the Controller ... I used ng-model = 'quantity' and tried to recover via {{quantity}}, which obviously did not work.

Can you help me?

My code: link

Thank you! =)

    
asked by anonymous 23.10.2015 / 16:17

2 answers

1

Create a variable in the controller for that ng-model and get the value of $scope.quantidade , then just pass the variable to the function.

       //Pega o valor do input
        var quantidade = $scope.quantidade;
    
23.10.2015 / 16:38
0

Here is a 100% functional example of how you can retrieve the data entered via javascript.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript">
   function ok() {
      var scope = $('#entityScope').scope();
      alert(scope.firstName);
   }
</script>
<body>

<p>Try to change the names.</p>

<div id="entityScope" ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
<br>
<input type="button" value="Ok" onclick="ok()">
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
});
</script>

</body>
</html>
    
23.10.2015 / 22:57