这是我的代码:
<!doctype html>
<html lang="en-US" ng-app>
<!--Head-->
<head>
<meta charset="UTF-8">
<title>Lesson 5 - ng-show & ng-hide</title>
<meta name="viewport" content="width=device-width">
<style type="text/css">
* {
box-sizing: border-box;
}
body {
font: 16px/1.5 Consolas;
color: #222;
margin: 5em;
}
</style>
</head>
<!--Body-->
<body>
<div ng-controller="information">
<!--Input-->
<label for="name">
Name:
<input type="text" name="username" id="username" placeholder="Your name here please" ng-model="name"/>
</label>
<br>
<label>
Hide?
<input type="checkbox" ng-model="checked"/>
</label>
<!--Div that is hidden-->
<div ng-hide="checked">
Hidden Message here
<br>
Welcome {{ name || "user" }}!
</div>
</div>
<script type="text/javascript" src="angular_1.0.7.js"></script>
<script type="text/javascript">
var information = function ($scope) {
$scope.$watch(function () {
console.log($scope.name);
});
};
</script>
</body>
</html>
如果您运行这个并更改<input>
标签内的值,那么您将在控制台窗口中看到,每次更改值时,您更改的值都会输出两次,如下所示:
为什么会这样?