1

Can anyone tell me how can I do that please?

<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.0-rc.2/angular.js" data-semver="1.4.0-rc.2"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <form name="form.one" ng-submit="submitForm()">
      <input ng-model="name">
      <input type="submit">
    </form>
    <p>Hello {{name}}!</p>


  </body>

And my app.js is

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

app.controller('MainCtrl', function($scope) {
$scope.name = "Name";
var data = {};
$scope.submitForm = function() {

 var data = form.one.name;
};

});

How can I save input to the data variable? Is it possible to save on keypress?

3
  • 1
    The input is in $scope.name. Why would you want it in another variable? Commented May 19, 2015 at 16:04
  • ng-model provides two way binding. Just have a look for ngModel. Also check this doc listing all input arguments for better understanding. Commented May 19, 2015 at 16:06
  • I want to pass this input result as a $http url, so this why I was thinking to use another variable, I'm just starting with angularjs and programming. Commented May 19, 2015 at 16:52

2 Answers 2

4

To use your form with Angular, there's a few modifications you need to make for this code to work: first, you need to add the novalidate attribute to your form; it is used to disable the browser's native form validation. Angular will use it's own validation. Here's a few other modifications (they're explained in detail here):

<body ng-controller="MainCtrl">
  <form novalidate>
    <input ng-model="name">
    <!-- The method for submitting the data goes on the button. -->
    <!-- The name is passed to the method you want to use to store the data, which you make happen when the button is clicked by using the ngClick directive. -->
    <input type="submit" ng-click="submitForm(name)" value="Click me!">
  </form>
  <p>Hello {{name}}!</p>
  <!-- This bit of code is to display the results of adding names to the array in the browser window: -->
  <pre>{{ data }}</pre>
</body>

Here's the Javascript:

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

app.controller('MainCtrl', function($scope) {
  $scope.name = "Name";

  // I put the data array in the $scope so it can be displayed in the browser window and you can see the results:
  $scope.data = [];

  // Now, whenever the button is clicked, this method is run.
  // It then stores the name in the 'data' array defined above.
  $scope.submitForm = function(name) {
    $scope.data.push(name);
  };
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much, it is possible to do this without clicking the button? On typing like this {{name}} is showing strait after typing?
Do you mean adding to the array straight after typing or displaying in the browser straight after typing? For displaying the name as the user types, all you have to do is make your your form input and paragraph element to refer to the same variable in the scope, such as $scope.name.
yes, in original idea was take data from input and add to the end of $http url so I can get json response from the server live on typing, this why I want to use different variable to store this data. $http.get('server.com/$scope.name').success(function (response) {$scope.name2 = response;});
Ah, I see. The answer to that is different and a bit more complex. Perhaps it would be more useful to you to create a new question for the specific behaviour you're trying to achieve?
Ok there's the question stackoverflow.com/questions/30333578/… Thanks Ricardo for your help
0

Try this for it to show straight after typing:

<body ng-controller="myApp">
    <input type="text" ng-model="name">
    <p>Hello {{name}}!</p>
</body>

Javascript:

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

myApp.controller('myApp', function ($scope) {
    $scope.name = $scope.name;
    console.log($scope.name)
});

Small little JSFiddle for this: https://jsfiddle.net/joshdmiller/HB7LU/

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.