今天先讲一个angularJs的表单绑定实例:
<div ng-app="myApp" ng-controller="formCtrl"> <form novalidate> 姓:<input type="text" ng-model="user.xing"><br> 名:<input type="text" ng-model="user.ming"><br> <button ng-click="reset()">RESET</button> </form> <h2 style="color:#F00">新值 = {{user }}</h2> <h2>原值 = {{master}}</h2> </div> <script> var app = angular.module('myApp', []); app.controller('formCtrl', function($scope) { $scope.master = {xing:"张", ming:"三"}; $scope.reset = function() { $scope.user = angular.copy($scope.master); }; $scope.reset(); }); </script>
注:novalidate 属性是在 HTML5 中新增的。禁用了使用浏览器的默认验证。
接下来写两个angularJs循环列举数组
<div ng-app=“” ng-init=“car=[‘宝马’,‘奥迪’,‘奔驰‘,’保时捷’,’法拉利’]"> <p>使用 ng-repeat 来循环数组</p> <ul> <li ng-repeat="x in car"> {{ x }} </li> </ul></div>
<div ng-app=“” ng-init=“names=[{name:‘王小波’,class:‘傻子班’},{name:‘王如花’,class:‘大班’},{name:‘武则天’,class:‘预科班'}]"> <ul> <li ng-repeat="x in names"> {{ x.name + ', ' + x.class }} </li> </ul> </div>