Input value not visible in angularJS

10,555

You cannot mix regular HTML (the value attribute of your input) and the angular-driven ng-model. The input will always display the value of what's in ng-model, here lieu, hence you should iniialize $scope.lieu with the Paris value.

(function(angular, undefined) {
  var mon_app = angular.module('mon_app', []);
  angular.module('mon_app').controller('monctrl', function($scope) {
    $scope.lieu = "Paname";
  })
}(window.angular));
<!DOCTYPE html>
<html class="app" ng-app="mon_app">

<head lang="fr" class="app">
</head>

<body>
  <div ng-controller="monctrl">
    This input value is "{{lieu}}"
    <input name="lieu" ng-model="lieu" type="text"/>
  </div>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.js"></script>
</body>

Share:
10,555
Papa Sax
Author by

Papa Sax

Updated on July 01, 2022

Comments

  • Papa Sax
    Papa Sax almost 2 years

    This input value should be "Paris" but it does not display. What is wrong with my code?

    (function(angular, undefined) {
      var mon_app = angular.module('mon_app', []);
      angular.module('mon_app').controller('monctrl', function($scope) {})
    }(window.angular));
    <!DOCTYPE html>
    <html class="app" ng-app="mon_app">
    
    <head lang="fr" class="app">
    </head>
    
    <body>
      <div ng-controller="monctrl">
        This input value is "Paris"
        <input name="lieu" ng-model="lieu" type="text" value="Paris" />
      </div>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.js"></script>
    </body>

    • PSL
      PSL over 9 years
      It wont work that way. You need to set $scope.lieu="paris" in your controller to set default value
    • akonsu
      akonsu over 9 years
      or you can use ng-init
  • floribon
    floribon over 9 years
    @user2761037 If that helped you please +1 and/or accept the answer :)
  • Papa Sax
    Papa Sax over 9 years
    I would love to, but i actually created my stackoverflow account to ask this question. Vote Up requires 15 reputation.
  • floribon
    floribon over 9 years
    I see. Let's call it, échange de bon procédé ;-)