I am a newbie to angular js and I have just started learning it. I was been going through w3schools tutorials and I got stuck in forms part. The reason being is below:
The code given in tutorial is as below and it works fine:
<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>
But what I am trying is separation of modules as shown in one of their tutorail but that doesn't work at all. Below is what I tried.
<!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 contains below code
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();
});
and myApp.js is as below:
var app=angular.module("myApp",[]);
But when I try the tutorial code it gives me below output
form = {"firstName":"John","lastName":"Doe"} master = {"firstName":"John","lastName":"Doe"}
and when I run what I tried it gives below output
form = {{user}}
master= {{master}}
Why is this happening so? I have also tried keeping angular.js link at the top but still the result is same..