AngularJS ng-model 指令

AngularJS ng-model 指令


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


ng-model 指令

ng-model 指令可以将输入域的值与 AngularJS 创建的变量绑定。

AngularJS 实例

< 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 >

尝试一下 »

双向绑定

双向绑定,在修改输入域的值时, AngularJS 属性的值也将修改:

AngularJS 实例

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

尝试一下 »

验证用户输入

AngularJS 实例

< form  ng-app= ""  name= "myForm" >
    Email:
     < input  type= "email"  name= "myAddress"  ng-model= "text" >
     < span  ng-show= "myForm.myAddress.$error.email" >不是一个合法的邮箱地址 < /span >
< /form >

尝试一下 »

以上实例中,提示信息会在 ng-show 属性返回 true 的情况下显示。


应用状态

ng-model 指令可以为应用数据提供状态值(invalid, dirty, touched, error):

AngularJS 实例

< form  ng-app= ""  name= "myForm"  ng-init= "myText = 'test@runoob.com'" >
    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 类:

AngularJS 实例

< style >
input.ng-invalid {
     background-color:  lightblue;
}
< /style >
< body >

< form  ng-app= ""  name= "myForm" >
    输入你的名字:
     < input  name= "myAddress"  ng-model= "text"  required >
< /form >

尝试一下 »

ng-model 指令根据表单域的状态添加/移除以下类:

  • ng-empty
  • ng-not-empty
  • ng-touched
  • ng-untouched
  • ng-valid
  • ng-invalid
  • ng-dirty
  • ng-pending
  • ng-pristine
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Siemens Opcenter 页面中使用 ng-model 指令,需要先引入 AngularJS 库。然后,在 HTML 元素中使用 ng-model 指令来绑定数据模型并实现双向数据绑定。具体步骤如下: 1. 在 HTML 页面中引入 AngularJS 库,可以使用如下代码: ```html <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> ``` 2. 在 HTML 元素中使用 ng-model 指令来绑定数据模型,例如: ```html <input type="text" ng-model="username"> ``` 这个例子中,ng-model 指令绑定了一个叫做 username 的数据模型,并将其与一个文本框进行了关联。 3. 在 JavaScript 中,可以使用 $scope 对象来控制 ng-model 指令所绑定的数据模型。例如: ```javascript var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.username = "John Doe"; }); ``` 这个例子中,$scope 对象控制了一个叫做 username 的数据模型,并将其初始化为 "John Doe"。 4. 最后,在 HTML 元素中使用 ng-controller 指令来指定控制器,例如: ```html <div ng-app="myApp" ng-controller="myCtrl"> <input type="text" ng-model="username"> <p>Hello {{ username }}!</p> </div> ``` 这个例子中,ng-app 指令定义了一个名为 myApp 的 AngularJS 应用程序,ng-controller 指令指定了一个名为 myCtrl 的控制器。在控制器中,$scope 对象控制了一个名为 username 的数据模型。在 HTML 中,ng-model 指令将文本框与 username 数据模型进行了绑定,同时使用 {{ }} 语法来显示数据模型的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李晓LOVE向阳

你的鼓励是我持续的不断动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值