AngularJSを初めて使用し、フォーム名を使用してフォームフィールドのデフォルト値を設定しようとしています。私は入力フィールドに入力すると何が間違っているのかわからないので、私は下の見出しの値を見ることができます。しかし、デフォルト値はjavascriptコードで設定されていません。デフォルトでanglejsにフォームフィールド値を設定できません
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<form name="testForm">
<input ng-model="testForm.name">
<h1>My name is {{testForm.name}}</h1>
</form>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.testForm = {};
$scope.testForm.name = "John Doe";
});
</script>
<p>When you change the name in the input field, the changes will affect the model, and it will also affect the name property in the controller.</p>
</body>
</html>
ありがとう、これは働いた。しかし、なぜ私たちはこれを行う必要がありますか...私はそれが$範囲で直接動作しなかった理由を意味します。 –
このhttps://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#controllersをお読みください –