angularjs入门例子

html

<!DOCTYPE html>
<html ng-app="myApp" >
	<head>
		<meta charset="utf-8">
		<title>学习</title>
		<script src="js/angular.min.js"></script>
		<script src="js/a1.js"></script>
	</head>
<body>
 
<div   ng-controller="personController">

名: <input type="text" ng-model="person.firstName"><br>
姓: <input type="text" ng-model="person.lastName"><br>
<br>
姓名: {{person.firstName + " " + person.lastName}}
<br />
 
<button  name="btn_1" ng-click="person.xhello()" >提示一下</button>
</div>
<br />
<br />
<div    ng-controller="MyController">
	<button  name="btn_2" ng-click="sayHello()" >调ajs函数</button>
</div>

<br />
 
<button ng-init="shouldShow = true" ng-click="shouldShow = !shouldShow">Flip the shouldShow variable</button>
 <div ng-show="shouldShow">
   <h3>Showing {{ shouldShow }}</h3>
 </div> 
 <br />
 <div ng-hide="shouldShow">
   <h3>Hiding {{ shouldShow }}</h3>
 </div>
 <br />
 <br />
 <div ng-controller="namesController">
<ul>
  <li ng-repeat="x in names  | orderBy:'country' ">
    {{ x.name   + ', ' + x.country }}  ... {{x.name  |   uppercase }}
  </li>
</ul>
</div>
<br />
事件
<div controller="myController">
<button ng-click="count = count + 1">点我!</button>
<p>{{count}}</p>
</div>
<br />
<br />
表单验证

<h2>Validation Example</h2>

<form  ng-app=""  ng-controller="validateCtrl"
name="myForm" novalidate>

<p>Username:<br>
  <input type="text" name="user" ng-model="user" required>
  <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
  <span ng-show="myForm.user.$error.required">Username is required.</span>
  </span>
</p>

<p>Email:<br>
  <input type="email" name="email" ng-model="email" required>
  <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
  <span ng-show="myForm.email.$error.required">Email is required.</span>
  <span ng-show="myForm.email.$error.email">Invalid email address.</span>
  </span>
</p>

<p>
  <input type="submit"
  ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
  myForm.email.$dirty && myForm.email.$invalid">
</p>

</form>

</body>
</html>

a1.js


var myApp = angular.module('myApp',[]);

 
myApp.controller('MyController', function($scope) {
   var count=0;
   $scope.sayHello = function() {
   	 console.log("------MyController.sayHello()");
     alert("hello!");
   }
});

myApp.controller('personController', function($scope) {
    //person = this;
    $scope.person = {
        firstName: "John",
        lastName: "Doe"
    };
    $scope.person.xhello = function(){
    	alert("xx");
    	console.log('-----------测试');
    	 
    };
});

myApp.controller('namesController', function($scope) {
    $scope.names = [
        {name:'Jani',country:'Norway'},
        {name:'Hege',country:'Sweden'},
        {name:'Kai',country:'Denmark'}
    ];
});

myApp.controller('validateCtrl', function($scope) {
   $scope.user = 'John Doe';
   $scope.email = 'john.doe@gmail.com';
});

 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值