0

I'm new to Angular and Ionic and I have problem with getting value of input. I get "undefined". Here is my code:

.controller('myCtrl', function($scope) {

  $scope.submit = function () {
    console.log($scope.name);
  }
  
}
 <form ng-submit="submit()">
   <input type="text" ng-model="name">
  
   <button class="button">Send</button>
 </form>

3
  • What is error text? Commented Nov 8, 2016 at 12:12
  • you are missing ); after your controller declaration Commented Nov 8, 2016 at 12:20
  • Sure, but I just lose it while pasting code ;) Commented Nov 8, 2016 at 12:28

3 Answers 3

0

try this

 <form>
   <input type="text" ng-model="name">

   <button ng-click="submit()" class="button">Send</button>
 </form>
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work :( submit() is running but still I get "undefined" text
It works in your code but not in mine. I have identical code.
0

Try this code

<form ng-submit="submit()">
   <input type="text" ng-model="FinalData.name" name="name">
    <button class="button">Send</button>
 </form>

And then in controller

   $scope.FinalData={};

   $scope.FinalData.name ="dasda";

   $scope.submit = function(){

   console.log($scope.FinalData.name);

   }

3 Comments

It works when I do $scope.name="asdasd" but I need to get it from input value.
Then dont assign value to FinalData.name..pass empty string.
It's not empty... Nevemind, I have don't this width pure javascript and it works.
0

Check with this example

angular.module('myApp', []).controller('myCtrl', function($scope) {
    $scope.name = 'Hello World!';
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller='myCtrl'>

<form ng-submit="submit()">
   <input type="text" ng-model="name">
  
   <button class="button">Send</button>
 </form>

</div>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.