AngularJS:ng作用域、ng过滤器、ng服务、

本文深入讲解AngularJS框架的关键特性,包括作用域管理、内置过滤器如currency、lowercase、filter、orderBy、uppercase的使用,以及服务如$location、$http、$timeout、$interval的应用,还有HTTP AJAX异步通讯的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ng作用域:


**

$scope.username='user456';
$scope.say=function(){
	return 'abc';
}

ng过滤器:

currency 	 #格式化数字为货币格式。
filter 	#从数组项中选择一个子集。
lowercase 	#格式化字符串为小写。
orderBy 	#根据某个表达式排列数组。
uppercase 	#格式化字符串为大写。

currency货币过滤器

<div class="form-group">
			<label for="">用户名:</label>
			<input type="text" class="form-control" ng-model='username'>
</div>
<div class="well">用户名: {{username|currency}}</div>

lowercase转小写过滤器

<div class="form-group">
			<label for="">用户名:</label>
			<input type="text" class="form-control" ng-model='username'>
</div>
<div class="well">用户名: {{username|lowercase}}</div>

filter过滤器

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<div class="form-group">
			<input type="text" class="form-control" ng-model='find'>
		</div>
		<div class="list-group">
			<a href="" class="list-group-item" ng-repeat='row in rows|filter:find'>{{row}}</a>
		</div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope){
	$scope.rows=['user1','linux2','php3','java4','user5'];
});
</script>

orderBy过滤器

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<div class="form-group">
			<input type="text" class="form-control" ng-model='find'>
		</div>
		<div class="list-group">
			<a href="" class="list-group-item" ng-repeat='row in rows|filter:find|orderBy:"id"'>{{row.name}}</a>
		</div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope){
	$scope.rows=[
		{'name':'user110','id':1},
		{'name':'linux','id':30},
		{'name':'user10','id':2},
		{'name':'phpu2','id':40},
		{'name':'javu30','id':4}
	];
});

</script>

uppercase转大写过滤器

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
		<!-- mvc -->

		<div class="form-group">
			<label for="">用户名:</label>
			<input type="text" class="form-control" ng-model='username'>
		</div>
		<div class="well">用户名: {{username|uppercase}}</div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope){
});
</script>

ng服务:

1.$location
2.$http
3.$timeout
4.$interval

**

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<div class="well">url: <span class='label label-success'>{{url}}</span></div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope,$location){
	$scope.url=$location.absUrl();
});

</script>

timeout超时器服务

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<div class="well">url: <span class='label label-success'>{{url}}</span></div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope,$location,$timeout){
	$timeout(function(){
		$scope.url=$location.absUrl();
	},2000);
});

</script>

$interval服务

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<div class="well">url: <span class='label label-success'>{{num}}</span></div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope,$interval){
	$scope.num=0;

	s=0;
	v=1;

	$interval(function(){
		s+=v;
		$scope.num=s;
	},1000);
});

</script>

http ajax异步通讯

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<div class="well">url: <span class='label label-success'>{{num}}</span></div>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope,$http){
	$http.get('get.php').success(function(res){
		if(res=='ok'){
			$scope.num='1';
		}else{
			$scope.num='0';
		}
	});
});

</script>

get.php

<?php
echo "ok"
?>

http ajax获取json字符串

<body ng-app='myapp' ng-controller='myctl'>
	<div class="container">
		<h1 class="page-header">angular框架</h1>
				
		<table class='table table-striped table-bordered table-hover'>
			<tr>
				<th>名称:</th>
				<th>地址:</th>
				<th>国家:</th>
			</tr>	
			<tr ng-repeat='site in sites'>
				<td>{{site.Name}}</td>
				<td>{{site.Url|uppercase}}</td>
				<td>{{site.Country|lowercase}}</td>
			</tr>
		</table>
	</div>
</body>
<script>
app=angular.module('myapp',[]);

app.controller('myctl',function($scope,$http){
	$http.get('get.php').success(function(res){
		$scope.sites=res.sites;
	});
});

</script>

get.php

<?php 
$str='{
    "sites": [
        {
            "Name": "菜鸟教程",
            "Url": "www.runoob.com",
            "Country": "CN"
        },
        {
            "Name": "Google",
            "Url": "www.google.com",
            "Country": "USA"
        },
        {
            "Name": "Facebook",
            "Url": "www.facebook.com",
            "Country": "USA"
        },
        {
            "Name": "微博",
            "Url": "www.weibo.com",
            "Country": "CN"
        }
    ]
}';

echo $str;
 ?>

**


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值