本篇文章介绍了关于angularjs的简述中级篇,介绍了单页web应用,三种模板方式,$scope,作用域,遍历,其他指令,请求数据jqLite,$watch,$apply。接下来就让我们一起来看这篇文章吧
单页面应用程序的特点:整个网站由一个页面构成,公共部分只加载一次,利用Ajax局部刷新达到页面切换的目的,不会发生页面跳转白屏的现象,锚点与页面对应单页web应用的应用。
场景:单页面应用对搜索引擎不友好,不适合做面向大众的展示型网站,网站后台管理系统、办公OA、混合App等等不需要被搜索引擎搜索到的应用
<script src="node_modules/angular/angular.min.js"></script>
<script src="node_modules/angular-route/angular-route.min.js"></script>
<body ng-app="myApp">
<a href="#!/index">首页</a>
<a href="#!/list">列表页</a>
<div ng-view></div>
</body>
<script>
var app=angular.module('myApp',['ngRoute'])
app.config(function($routeProvider){
$routeProvider
.when('/index',{
templateUrl:'./tpl/index.html',
controller:'indexCtrl'
})
.when('/list',{
templateUrl:'./tpl/list.html',
controller:'listCtrl'
})
.otherwise('/index')
});
app.controller('indexCtrl',function($scope){
$scope.msg="我是首页msg"
})
app.controller('listCtrl',function($scope){
$scope.msg="我是列表页msg"
}) </script>三种模板方式
<script>
templateUrl:'./tpl/index.html'//localhost
template:'<div>我是首页</div>'//file|localhosst
template:'indexTpl'//file|localhosst</script>$scope
可以传递的参数有很多,不需要一一写出来
angularjs中传递参数不能依靠顺序而是名字
如果形参名字改变了,angularjs就不知道要干什么了
解决方法:第二个参数写数组,回调函数放到数组中
压缩的时候,不会对字符串进行压缩,所以数组中传递字符串来确定参数的顺序
<script>
angular.module("myApp",[]).controller("demoCtrl",["$scope","$timeout","$http","$location",function(a,b,c,d){
a.msg="我是msg"
}]) </script>作用域
作用域就近原则
angularjs中控制器控制的区域就是一个局部作用域,
也就是$scope代表局部作用域
$rootScope代表全局作用域
变量先顺着$scope找,找不到就去全局找
可以挂载公共属性方法
遍历
ng-repeat="循环过程中的当前项 in 数据"循环数据并生成当前DOM元素
<ul>
<li ng-repeat="item in arr">{{item}}</li>
</ul>遍历数组对象,可以嵌套,有ng-repeat的标签中还可以嵌套ng-repeat的标签
<ul>
<li ng-repeat="person in person">
{{person.name}}{{person.age}}
<span ng-repeat="item in person.hobby">{{item}}</span>
</li>
</ul>
数组项重复,会报错
<ul>
<li ng-repeat="item in arr track by $index">{{item}}</li>
</ul>其他指令
ng-class="{'类名1':布尔值,'类名2':布尔值}"专门用来添加或者删除类名,接收的值是一个对象,布尔值为真,添加类名,布尔值为假,删除类名
复选框,ng-model用来获取复选框的值,是一个布尔值
ng-bind="数据",将msg放到属性中进行加载,避免出现闪烁效果
ng-bind-template="{{数据1}} {{数据2}}"
ng-non-bindable直接得到插值表达式中的内容,只要与属性相关,都不执行
ng-show="布尔值",控制元素的显示和隐藏
ng-hide="布尔值",控制元素的显示和隐藏
ng-if="布尔值",控制元素的显示和隐藏 true 显示 false 隐藏
ng-switch&ng-switch-when用法和switch-case类似
事件指令
onclick => ng-click
onmouseenter => ng-mouseenter
onchange => ng-change
ng-dblclick 双击事件
ng-src没有src就不会解析就不会报错,直到angularjs加载完成,解析ng-src之后再生成src
ng-href同上
ng-options用来循环下拉列表,不能单独使用,需要配合ng-model一起使用
请求数据
要请求数据需要先引入js文件
引入的js文件作为依赖文件,控制器中必须写入$http
$http-->请求的地址,相当于jQuery中的ajax
method-->type请求的方式
params-->data只用于get传参
data可以用于post传参
$http点then后面是两个回调函数
第一个回调函数是成功回调
第二个回调函数是失败回调
res是形参,表示请求回来的数据
<script src="node_modules/angular/angular.js"></script>
<script src="node_modules/angular-sanitize.min.js"></script>
<script>
angular.module('myApp',['ngSanitize'])
.controller('demoCtrl',['$scope','$http',function($scope,$http){
$http({ url:'./test.json', method:'post',//请求方式
params:{//get传参
a:1, b:2
}, data:{ c:3, d:4
}
}).then(function(res){ //成功回调
console.log(res)
},function(){ //失败回调
}) //另外一种写法
$http.get('./test.json',{params:{a:1,b:2}}).then(function(res){ //get方式传参
console.log(res)
})
$http.post('./test.json',{c:3,d:4}.then(function(res){ //post方式传参
console.log(res)
})
}]) </script>jqLite
为了方便DOM操作,angularjs提供了一个jQuery精简版,叫做jqLite
$(原生的JS对象)将原生JS对象转换成jQuery对象,目的是为了使用jQuery对象下面提供的方法
angularjs.element(原生JS对象)将原生JS对象转换成jqLite对象,目的是为了使用jqLite对象下面提供的方法
这里angularjs.element相当于jQuery中的$
jqLite中方法的使用和jQuery高度相似
$watch
$watch用来监控数据的变化
第一个参数是要监控的数据,第二个参数是回调函数
回调函数中第一个参数newValue是用户输入的最新内容,第二个参数oldValue是上一次用户输入的内容
页面一上来的时候,回调函数会先执行一次
<script>
$scope.$watch('val',function(newValue,oldValue){ if(newValue.length>10){
$scope.tips="大于10";
}else{
$scope.tips="小于10"
}
}) </script>$apply
当通过原生JS将angularjs中的数据做了改变以后,angularjs不知道,所以需要调用$apply()方法通知angularjs更新html页面
以上就本篇关于angularjs简历的中级篇文章了,下一篇终极的在后面,大家期待吧,想学更多关于angularjs的相关知识就到PHP中文网angularjs参考手册栏目中学习。
以上就是angularjs的基本介绍你了解多少?这里有angularjs的详细介绍的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号