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>
Author by
Papa Sax
Updated on July 01, 2022Comments
-
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 over 9 yearsIt wont work that way. You need to set
$scope.lieu="paris"
in your controller to set default value -
akonsu over 9 yearsor you can use
ng-init
-
-
floribon over 9 years@user2761037 If that helped you please +1 and/or accept the answer :)
-
Papa Sax over 9 yearsI would love to, but i actually created my stackoverflow account to ask this question. Vote Up requires 15 reputation.
-
floribon over 9 yearsI see. Let's call it, échange de bon procédé ;-)