【angularJS】学习笔记
一、一个html中多个ng-app
//对于ng-app初始化一个AngularJS程序属性的使用需要注意,在一个页面中AngularJS自动加载第一个ng-app,其他ng-app会忽略
//如果需要加载其他ng-app程序,需要手动添加初始化过程。
angular.bootstrap(document.getElementById("div2"), ['myApp2']);
angular.bootstrap(document,module,config); 正常参数是三个
第一个是dom对象,
第二个是模型对象,
第三个是配置。
这个module的名称可自己定义。
<div ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="first"/>
<input type="text" ng-model="last"/>
<br/>
姓名:{{ first + " " + last}}
</div>
<div id="userForm" ng-app="userForm" ng-controller="userController">
<input type="text" ng-model="username"/><br/>
<input type="text" ng-model="password"/><br/>
用户名:{{username}}<br/>
密码: {{password}}
</div>
<script type="text/javascript">
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.first = "test";
$scope.last = "test";
});
var userApp = angular.module("userForm",[]);
userApp.controller("userController",function($scope){
$scope.username = "test";
$scope.password = "test";
});
angular.bootstrap(document.getElementById("userForm"),['userForm']);
</script>
二、select选择框
AngularJS 可以使用数组或对象创建一个下拉列表选项。
使用 ng-options 指令来创建一个下拉列表,列表项通过对象和数组循环输出,
也可以使用ng-repeat 指令来创建下拉列表。
对比:
<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select>
<h1>你选择的是: {{selectedSite}}</h1> //selectedSite结果是x.url <select ng-model="selectedSite" ng-options="x.site for x in sites">
</select>
<h1>你选择的是: {{selectedSite.site}}</h1> //selectedSite结果是x
<p>网址为: {{selectedSite.url}}</p>
ng-repeat 指令 是通过数组来循环 HTML 代码来创建下拉列表,
但 ng-options 指令 更适合创建下拉列表,它有以下优势:
使用 ng-options 选择的值是一个对象, ng-repeat 选择的值是一个字符串。
当选择值是一个对象时,我们就可以获取更多信息,应用也更灵活。
ngOptions用法详解,参考:https://www.cnblogs.com/laixiangran/p/4956751.html
ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上。
- 数组:
label for value in array
select as label for value in array
label group by group for value in array
select as label group by group for value in array
select as label group by group for value in array track by trackexpr
- 对象:
label for ( key , value ) in object
select as label for ( key , value ) in object
label group by group for ( key , value ) in object
select as label group by group for ( key , value ) in object
说明:
array / object: 数据源的类型,有数组和对象两种
value:迭代过程中,引用数组的项或者对象的属性值
key:迭代过程中,引用对象的属性名
label:选项显示的标签,用户可看到的
select:结果绑定到ngModel中,如果没有指定,则默认绑定value
group:group by的条件,表示按某条件进行分组
trackexpr:用于唯一确定数组中的迭代项的表达式
举例数组
$scope. supplierList = [
{ "ID": 0, "SupplierCode": 0, "SupplierName": "全部" },
{ "ID": 1, "SupplierCode": 0, "SupplierName": "金飞" },
{ "ID": 2, "SupplierCode": 0, "SupplierName": "喷飞" },
];
ng-options="item.ID as item.SupplierName for item in supplierList"
三、Table表格
ng-repeat 指令可以完美的显示表格。
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
如果需要对表格进行排序,我们可以添加 orderBy 过滤器:
<tr ng-repeat="x in names | orderBy : 'Country'">
angularjs table,点击行tr获取tr行数据
<tr ng-repeat="value in fahrplanList" ng-click="outBoundTableClick(this)">
$scope.outBoundTableClick = function (index) {
$scope.outBountData = $scope.outBoundDataList[index.$index];
}
也可以试下:
ng-click="setMemberEntityValue($index)"
$scope.setMemberEntityValue = function (index) {
var tds = $("input[name='member']").eq(index).parent().parent().find('td');
四、http post数据
1、数据传递问题:后端接收不到AngularJs中$http.post发送的数据,总是显示为null
检查了下,后台的模型,需要注意:
1、可序列化[DataContract(IsReference = true)] 、和用属性的方式,不要用字段
2、属性要和前端传递的完全一样。
Eg:
[DataContract(IsReference = true)]
public class CKSVoyageSearchSelectModel
{
/// <summary>
/// 前端路由key
/// </summary>
[DataMember]
public string rkey { set; get; }
2、post多个参数
http.post 多个参数时:写在一块{}
Js代码:
var params = {
rkey: $scope.rkey,
rq: {
Head: {
Auth: "string",
Errcode: 0,
Errormessage: ""
},
Data: {
DepartureCode: $scope.condition.departure_code,
},
}
};
offlineBookingService.searchVoyage(params).then(function (rs) {
后台控制器代码:
[FrontAuthenticationAttrbute]
[HttpPost]
public ActionResult SearchVoyage(CommonBO<VoyageRequest> rq,string rkey)
{}
//不要出现有重载的函数
public ActionResult SearchVoyage(CommonBO<VoyageRequest> rq)
{}
五、输入验证
AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告。
参考:https://www.cnblogs.com/zml-java/p/5644260.html
|
属性 |
描述 |
|
$dirty |
表单有填写记录 |
|
$valid |
字段内容合法的 |
|
$invalid |
字段内容是非法的 |
|
$pristine |
表单没有填写记录 |
六、AngularJS API
AngularJS 全局 API 用于执行常见任务的 JavaScript 函数集合,如:
- 比较对象
- 迭代对象
- 转换对象
全局 API 函数使用 angular 对象进行访问。以下列出了一些通用的 API 函数:
|
API |
描述 |
|
angular.lowercase() |
转换字符串为小写 |
|
angular.uppercase() |
转换字符串为大写 |
|
angular.isString() |
判断给定的对象是否为字符串,如果是返回 true。 |
|
angular.isNumber() |
判断给定的对象是否为数字,如果是返回 true。 |
$scope.$watch 监控模型变量:
$scope.$watch('passw1',function() {$scope.test();});
$scope.$watch('passw2',function() {$scope.test();});
$scope.$watch('fName',function() {$scope.test();});
$scope.$watch('lName',function() {$scope.test();});
$scope.test = function() {
if ($scope.passw1 !== $scope.passw2) {
$scope.error = true;
} else {
$scope.error = false;
}
七、AngularJS 调试
参考:
Angular调试技巧——借助chrome上的Batarang插件
angular学习第一天——安装batarang踩到的那些坑儿
【angularJS】学习笔记的更多相关文章
- AngularJs学习笔记--Forms
原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...
- AngularJs学习笔记--expression
原版地址:http://code.angularjs.org/1.0.2/docs/guide/expression 表达式(Expressions)是类Javascript的代码片段,通常放置在绑定 ...
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
- AngularJs学习笔记--Guide教程系列文章索引
在很久很久以前,一位前辈向我推荐AngularJs.但当时我没有好好学习,仅仅是讲文档浏览了一次.后来觉醒了……于是下定决心好好理解这系列的文档,并意译出来(英文水平不足……不能说是翻译,有些实在是看 ...
- AngularJs学习笔记--bootstrap
AngularJs学习笔记系列第一篇,希望我可以坚持写下去.本文内容主要来自 http://docs.angularjs.org/guide/ 文档的内容,但也加入些许自己的理解与尝试结果. 一.总括 ...
- AngularJs学习笔记--html compiler
原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...
- AngularJs学习笔记--concepts(概念)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...
- AngularJS学习笔记2——AngularJS的初始化
本文主要介绍AngularJS的自动初始化以及在必要的适合如何手动初始化. Angular <script> Tag 下面通过一小段代码来介绍推荐的自动初始化过程: <!doctyp ...
- AngularJs学习笔记--Using $location
原版地址:http://code.angularjs.org/1.0.2/docs/guide/dev_guide.services.$location 一.What does it do? $loc ...
- AngularJs学习笔记--unit-testing
原版地址:http://docs.angularjs.org/guide/dev_guide.unit-testing javascript是一门动态类型语言,这给她带来了很强的表现能力,但同时也使编 ...
随机推荐
- OpenJDK自动安装脚本 InstallOpenJDK.vbs
Oracle JDK 要收费了,Open JDK没有安装包,只有Zip,写了个安装脚本 InstallOpenJDK.vbs Rem ********************************* ...
- kafka服务端实验记录
kafka单机实验: 环境准备: 1.下载kafka,zookeeper,并解压 wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.11 ...
- 在centos7.6上部署.netcore 3.0 web程序
首先需要一个全新的centos系统. 第一步:按照微软官方文档配置.netcore环境: https://dotnet.microsoft.com/download/linux-package-man ...
- C#的WebApi 与 EasyUi的DataGrid结合生成的可分页界面
1.从数据库每次取出的数据为当前分页的数据. 2.分页用的是EasyUI 的 Pagination控件,与DataGrid是相对独立的. 3.后台数据获取是通过WebApi去获取. 4.传入参数是:p ...
- JS中回调函数的简单用法
a能拿b,b能拿到c,c能拿到d,实现a拿到d的东西. function a() { b(function (data) { console.log(data); }); } function b(c ...
- Mongodb之简介
MongoDB是一个基于分布式存储的数据库,由C++语言编写,旨在为WEB应用提供的可扩展的高性能数据存储解决. MongoDB是介于关系型数据库与非关系型数据库之间的产品,也是非关系型数据库中功能最 ...
- 通过Nginx实现一个简单的网站维护通知页面
原文:https://www.zhyd.me/article/106 在网站发版时,总会有那么一小段时间服务是访问不通的,一般用户看到的都会是一个502的错误页面 那么可以通过nginx实现一个简单的 ...
- 传入json字符串的post请求
/** * 传入json字符串的post请求 * @Title: getRequsetData * @Description: TODO * @param @param url * @param @p ...
- 使用wc -l 来统计代码行数
Linux使用wc来统计行数真的好用 如在当前路径下统计一共多少行代码 $ find ./ -name *.c |xargs wc -l #包括了空行 ##-l :lines 如果不包括空行 ¥fin ...
- bcb中TParamter传NULL值
if (status_Desc.IsEmpty()) Queue_Status->Value = Null(); else Queue_Status->Value = status_Des ...