1

I just want to fill full name input box by inserting first name and last name input box. I just bind the data from above two values in input box, but it does not show in full name input box. Thanks in advance!! Code:

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

                First Name: <input type="text" ng-model="firstName"><br>
                Last Name: <input type="text" ng-model="lastName"><br>
                Full Name:<input ng-bind="firstName+" "+lastName>

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

2 Answers 2

1

You can't use ng-bind directive for HTML attributes, see the documentation.

ng-bind can be used in other HTML tags:

<span ng-bind="firstName + ' ' + lastName"></span>

With the input tag, you can use these directives:

<input type="text"
       ng-model="string"
       [name="string"]
       [required="string"]
       [ng-required="string"]
       [ng-minlength="number"]
       [ng-maxlength="number"]
       [pattern="string"]
       [ng-pattern="string"]
       [ng-change="string"]
       [ng-trim="boolean"]>

However, you can use the curly braces notation in the HTML value attribute:

<input value="{{firstName + ' ' + lastName}}">

Something like this:

(function() {
  var app = angular.module("myApp", []);
  app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
  });
}());
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

  First Name: <input type="text" ng-model="firstName"><br> Last Name: <input type="text" ng-model="lastName"><br> Full Name: <input value="{{firstName + ' ' + lastName}}">
  <span ng-bind="firstName + ' ' + lastName"></span>
</div>

Sign up to request clarification or add additional context in comments.

Comments

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

            First Name: <input type="text" ng-model="firstName"><br>
            Last Name: <input type="text" ng-model="lastName"><br>
            Full Name:<span ng-bind="fullName"></span>
     </div>

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

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.