Papa Sax
Papa Sax

Reputation: 919

Input value not visible in angularJS

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>

Upvotes: 4

Views: 11321

Answers (1)

floribon
floribon

Reputation: 19193

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>

Upvotes: 7

Related Questions