Page 90 - AngularJS权威教程
P. 90

70  第 9 章  内置指令


                 在线示例:http://jsbin.com/iQelOxi/1/edit。
                 19. ng-submit

                 ng-submit用来将表达式同onsubmit事件进行绑定。这个指令同时会阻止默认行为(发送请
             求并重新加载页面),除非表单不含有action属性。

                 <form ng-submit="submit()"
                       ng-controller="FormController">
                     Enter text and hit enter:
                     <input type="text"
                           ng-model="person.name"
                           name="person.name" />
                     <input type="submit"
                           name="person.name"
                           value="Submit" />
                     <code>people={{people}}</code>
                     <ul ng-repeat="(index, object) in people">
                         <li>{{ object.name }}</li>
                     </ul>
                 </form>

                 angular.module('myApp',[])
                 .controller('FormController',function($scope) {

                     $scope.person = {
                         name: null
                     };

                     $scope.people = [];

                     $scope.submit = function() {
                         if ($scope.person.name) {
                             $scope.people.push({name: $scope.person.name});
                             $scope.person.name = '';
                         }
                     };
                 });
                 在线示例:http://jsbin.com/ONIcAC/1/edit。

                 20. ng-class
                 使用ng-class  动态设置元素的类,方法是绑定一个代表所有需要添加的类的表达式。重复
             的类不会添加。当表达式发生变化,先前添加的类会被移除,新类会被添加。

                 下面的例子会用ng-class在一个随机数大于5时将.red类添加到一个div上:
                 <div ng-controller="LotteryController">
                     <div ng-class="{red: x > 5}"
                         ng-if="x > 5">
                         You won!
                     </div>
                     <button ng-click="x = generateNumber()"
                         ng-init="x = 0">
                         Draw Number
                     </button>
                     <p>Number is: {{ x }}</p>
                 </div>
   85   86   87   88   89   90   91   92   93   94   95