美文网首页
Angular (3)

Angular (3)

作者: big5 | 来源:发表于2016-07-22 16:21 被阅读13次

    AngularJS 模型

    ng-model 指令

    • ng-model 指令用于绑定应用程序数据到 HTML 控制器(input, select, textarea)的值。

       <div ng-app="myApp" ng-controller="myCtrl">   
        
            名字: <input ng-model="name">
       </div>
            
           <script>var app = angular.module('myApp', []);
       
              app.controller('myCtrl', function($scope) { 
               $scope.name ="John Doe";
              });
          
           </script>  
      
    • 双向绑定

        <div ng-app="myApp" ng-controller="myCtrl">
              名字: <input ng-model="name">   
             <h1>你输入了: {{name}}</h1>
        </div>
      
    • 验证用户输入

      <form ng-app="" name="myForm">
        Email:
        <input type="email" name="myAddress" ng-model="text">
        <span ng-show="myForm.myAddress.$error.email">不是一个合法的邮箱地址</span>
       </form>  
      
    • 响应状态
      ng-model 指令可以为应用数据提供状态值(invalid, dirty, touched, error):

      <form ng-app="" name="myForm" ng-init="myText = 'test@runoob.com'">
      <p> Email:
          <input type="email" name="myAddress" ng-model="myText" required>
      </p>
          <h1>状态</h1>
      {{myForm.myAddress.$valid}}
      {{myForm.myAddress.$dirty}}
      {{myForm.myAddress.$touched}}
      </form>  
      
    • css 类
      ng-model 指令基于它们的状态为 HTML 元素提供了 CSS 类:

      <style>
      input.ng-invalid {
          background-color: lightblue;
      }
      </style>
      <body>
      
      <form ng-app="" name="myForm">
          输入你的名字:
          <input name="myAddress" ng-model="text" required>
      </form>  
      
    • ng-model 指令根据表单域的状态添加或移除

      1. ng-empty
      2. ng-not-empty
      3. ng-touched
      4. ng-untouched
      5. ng-valid
      6. ng-dirty
      7. ng-pending
      8. ng-pristine

    相关文章

      网友评论

          本文标题:Angular (3)

          本文链接:https://www.haomeiwen.com/subject/wnyijttx.html