我是 Angular js 的新手,我刚刚开始学习它。我一直在学习 w3schools 教程,但我陷入了表单部分。原因如下:
教程中给出的代码如下,它工作正常:
<div ng-app="" ng-controller="formController">
<form novalidate>
First Name:<br>
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
<button ng-click="reset()">RESET</button>
</form>
<p>form = {{user}}</p>
<p>master = {{master}}</p>
</div>
<script>
function formController ($scope) {
$scope.master = {firstName: "John", lastName: "Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
};
</script>
但是我正在尝试的是分离模块,如他们的教程之一所示,但这根本不起作用。以下是我尝试过的。
<!DOCTYPE html>
<head>
<title>
Angular JS
</title>
</head>
<script src="Scripts/angular.min.js"></script>
<body>
<div data-ng-app="myApp" data-ng-controller="formController">
<form novalidate>
<table>
<tr>
<td>
First Name :
</td>
<td>
<input type="text" data-ng-model="user.firstName">
</td>
</tr>
<tr>
<td>
Last Name :
</td>
<td>
<input type="text" data-ng-model="user.lastName">
</td>
</tr>
<tr>
<button data-ng-click="reset()">Reset</button>
</tr>
</table>
</form>
<table>
<tr>
<td>
form = {{user}}
</td>
</tr>
<tr>
<td>
master= {{master}}
</tr>
</tr>
</table>
</div>
<script src="Scripts/myApp.js"></script>
<script src="Scripts/myCtrl.js"></script>
</body>
</html>
myCtrl.js包含以下代码
app.controller("myCtrl",function($scope)
{
$scope.firstName = "John",
$scope.lastName = "Doe"
$scope.myVar = false;
$scope.toggle = function() {
$scope.myVar = !$scope.myVar;
};
});
app.controller("formController",function($scope)
{
$scope.master = {firstName: "John", lastName: "Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
myApp.js如下:
var app=angular.module("myApp",[]);
但是当我尝试教程代码时,它给了我下面的输出
形式 = {"firstName":"John","lastName":"Doe"} 主 = {"firstName":"John","lastName":"Doe"}
当我运行我尝试过的东西时,它会给出以下输出
表单 = {{用户}}
主 = {{主}}
为什么会这样?我也尝试过将 angular.js 链接保持在顶部,但结果仍然相同..