Click here to Skip to main content
15,897,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
New in AngularJs. Finding it complex a little. I have created two text box and a button. Now I want, first I will write something inside the first text box. Then I will click on the button. After that the value of the first text box will get displayed inside the second text box. I have written very little code, I didn't even mentioned the button click function as I am confused on that. Please help me to go further.
XML
<script src="../../Scripts/angular.min.js" type="text/javascript"></script>
</head>
<body>
    <div ng-app="">
        <input type="text" ng-model="type">
        <br />
        <br />
        <input type="button" value="button" onclick="myFunction()" />
        <br />
        <br />
        <input type="text" ng-model="display" />
    </div>
</body>
Posted
Updated 27-Dec-16 2:00am

 
Share this answer
 
Here is the updated code

Html:

<div ng-app="myApp" ng-controller="myButtonCtrl">
<input type="text" ng-model="source">
<br />
<br />
<input type="button" value="button" ng-click="copyText(source);" />
<br />
<br />
<input type="text" ng-model="destination" />
</div>

and JavaScript:

PHP
var myButtonApp = angular.module("myApp", []);

        myButtonApp.controller("myButtonCtrl", [
            "$scope", function ($scope) {

                $scope.source = "Sample text to copy";
                $scope.destination = "";
                copyText = function (obj) {
                    alert(obj);
                    $scope.destination = obj;
                };
                $scope.copyText = copyText;
            }
        ]);
 
Share this answer
 
v2
HTML
<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
</head>

<body ng-app="textInputExample">
  <script>
    angular.module('textInputExample', [])
      .controller('ExampleController', ['$scope', function($scope) {
        $scope.example = {
          text: 'guest',
          word: /^\s*\w*\s*$/
        };
      }]);
  </script>
  <form name="myForm" ng-controller="ExampleController">
    <label>Single word:
      <input type="text" name="input" ng-model="example.text" ng-pattern="example.word" required ng-trim="false">
      <input type="text" name="copy" ng-pattern="example.word" value="{{example.text}}">
    </label>
    <div role="alert">
      <span class="error" ng-show="myForm.input.$error.required">

      Required!</span>
      <span class="error" ng-show="myForm.input.$error.pattern">

      Single word only!</span>
    </div>
    <code>text = {{example.text}}</code>
    <br/>
    <code>myForm.input.$valid = {{myForm.input.$valid}}</code>
    <br/>
    <code>myForm.input.$error = {{myForm.input.$error}}</code>
    <br/>
    <code>myForm.$valid = {{myForm.$valid}}</code>
    <br/>
    <code>myForm.$error.required = {{!!myForm.$error.required}}</code>
    <br/>
  </form>
</body>

</html>
<a href="https://plnkr.co/edit/YS8cX7AOGh3xd4aVroAN?p=preview">DEMO</a>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900