How to get value from a form with Angularjs?

Asked

Viewed 4,914 times

0

I’m starting with Angularjs and a question has arisen that seems to me to be simple.

I’m making a shopping cart (just for learning) with the little I’ve 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 caress. However, I would like to know how I took the value of the quantity and step to the function in Controller... I used ng-model='quantity' and tried to recover via {{ quantity }}, which obviously did not work.

Can you help me?

My code: https://jsfiddle.net/r7r99sr9/

Thank you! =)

2 answers

1


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

       //Pega o valor do input
        var quantidade = $scope.quantidade;

0

Here’s a 100% functional example of how you can rewind the data you entered via javascript.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src= "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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.