4

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>

2
  • 1
    It wont work that way. You need to set $scope.lieu="paris" in your controller to set default value Commented Jan 21, 2015 at 21:11
  • or you can use ng-init Commented Jan 21, 2015 at 21:12

1 Answer 1

7

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>

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

3 Comments

@user2761037 If that helped you please +1 and/or accept the answer :)
I would love to, but i actually created my stackoverflow account to ask this question. Vote Up requires 15 reputation.
I see. Let's call it, échange de bon procédé ;-)

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.