首页 > 代码库 > AngularJs之二

AngularJs之二

今天先讲一个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>

 

AngularJs之二