版权声明:本文为博主原创文章,未经博主允许不得转载。【合作联系QQ1668681286】 https://blog.csdn.net/qq_33036361/article/details/81988480
angular 笔记(二)
模块(module)、控制器(controller)、作用域(scope) 示例
<html ng-app="myApp">
<head>
<script src="angular.js" charset="utf-8"></script>
<script>
//创建一个模块 myApp
let app = angular.module('myApp', []);
//在模块 myApp 在创建一个控制器 main
app.controller('main',function($scope){
//$scope 作用域,页面绑定变量都保存在scope里
$scope.username='ken';//初始化username值为ken
//方法也是保存在scope
$scope.login=function(){
console.log($scope.username);
}
});
</script>
</head>
<body ng-controller="main">
<form name="myform">
用户名:<input type='text' ng-model='username' name='username' required/>
<span ng-show='myform.username.$error.required'>*必填项</span><br>
密 码:<input type='password' ng-model='pass' name='pass' required/>
<span ng-show='myform.pass.$error.required'>*必填项</span><br>
<input type='button' ng-click='login()' value='login'/>
</form>
</body>
</html>