Angular——单页面实例
基本介绍
1、引入的route模块可以对路由的变化做出响应
2、创建的控制器中依然需要$http向后台请求数据
3、php中二维数据的遍历用的是foreach
4、php中$arr=array(),$arr[] 可以被自动补充所缺失的索引
5、在ng-repeat中,如果数据相同的话会报错(ngRepeat:dupes),解决办法是后面添加track by $index===》list in lists track by $index
基本使用
html
<!DOCTYPE html>
<html lang="en" ng-app="Music">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body,ol,li{
margin: 0;
padding: 0;
}
.wrap{
width: 400px;
margin: 50px auto;
}
nav{
display: flex;
height: 40px;
border: 1px solid #000;
}
nav a{
flex: 1;
height: 40px;
text-decoration: none;
color: #000;
text-align: center;
line-height: 40px;
position: relative;
}
nav a:after{
content: '';
width: 0;
height: 35px;
border-left: 1px solid #000;
position: absolute;
top: 2.5px;
right: 0;
}
nav a:last-child:after{
visibility: hidden;
}
ol{
border: 1px solid #000;
border-top: none;
min-height: 300px;
padding-left: 40px;
}
ol li{
line-height: 40px;
}
</style>
</head>
<body>
<div class="wrap" ng-controller="IndexController">
<nav>
<a href="#/1">摇滚</a>
<a href="#/2">民族</a>
<a href="#/3">民谣</a>
<a href="#/4">公路</a>
</nav>
<ol>
<div ng-view=""></div>
</ol>
</div>
<script src="../libs/angular.min.js"></script>
<script src="../libs/angular-route.js"></script>
<script>
var Music=angular.module('Music',['ngRoute']);
Music.config(['$routeProvider',function ($routeProvider) {
$routeProvider.when('/:id',{
templateUrl:'./list.html',
controller:'IndexController'
}).otherwise({
redirectTo:'/1'
});
}]);
Music.controller('IndexController',['$scope','$http','$routeParams',function ($scope,$http,$routeParams) {
var type=$routeParams.id;
$http({
url:'./music.php',
method:'get',
params:{id:type}
}).success(function (info) {
$scope.lists=info;
});
}]);
</script>
</body>
</html>
模板页
<li ng-repeat="list in lists track by $index">{{list.text}}</li>
php
<?php
$pid=$_GET['id'];
$list = array(
array('id'=>1, 'pid'=>2, 'text'=>'我很丑可是我很温柔'),
array('id'=>2, 'pid'=>2, 'text'=>'浦公英的约定'),
array('id'=>3, 'pid'=>2, 'text'=>'你我的约定'),
array('id'=>4, 'pid'=>3, 'text'=>'pretty boy'),
array('id'=>5, 'pid'=>3, 'text'=>'See You Again'),
array('id'=>6, 'pid'=>2, 'text'=>'甜甜的'),
array('id'=>7, 'pid'=>1, 'text'=>'再见 我的爱人'),
array('id'=>8, 'pid'=>2, 'text'=>'心中的日月'),
array('id'=>8, 'pid'=>3, 'text'=>'Let It Go'),
array('id'=>10, 'pid'=>1, 'text'=>'不要说再见'),
array('id'=>11, 'pid'=>3, 'text'=>'Rise'),
array('id'=>12, 'pid'=>2, 'text'=>'再见'),
array('id'=>13, 'pid'=>1, 'text'=>'追梦人'),
array('id'=>14, 'pid'=>2, 'text'=>'不能说的秘密'),
array('id'=>15, 'pid'=>4, 'text'=>'고속도로 로맨스'),
array('id'=>16, 'pid'=>1, 'text'=>'昨日重现'),
array('id'=>17, 'pid'=>3, 'text'=>'Love Me Like You Do'),
array('id'=>18, 'pid'=>2, 'text'=>'好久不见'),
array('id'=>19, 'pid'=>1, 'text'=>'独角戏'),
array('id'=>20, 'pid'=>2, 'text'=>'K歌之王'),
array('id'=>21, 'pid'=>1, 'text'=>'往事随风'),
array('id'=>22, 'pid'=>2, 'text'=>'光辉岁月'),
array('id'=>23, 'pid'=>3, 'text'=>'Just Like Fire'),
array('id'=>24, 'pid'=>4, 'text'=>'涙の物语'),
array('id'=>25, 'pid'=>4, 'text'=>'江南Style'),
array('id'=>26, 'pid'=>4, 'text'=>'ガラガラ'),
array('id'=>27, 'pid'=>1, 'text'=>'海阔天空'),
array('id'=>28, 'pid'=>4, 'text'=>'天空之城'),
array('id'=>29, 'pid'=>4, 'text'=>'사랑이었다'),
array('id'=>30, 'pid'=>3, 'text'=>' Good Time'),
array('id'=>31, 'pid'=>1, 'text'=>'不再犹豫'),
array('id'=>32, 'pid'=>4, 'text'=>'さよならの夏 ~コクリコ坂から~'),
array('id'=>33, 'pid'=>3, 'text'=>' Heart And Soul'),
array('id'=>34, 'pid'=>4, 'text'=>'넘버나인 '),
array('id'=>35, 'pid'=>1, 'text'=>'往事只能回味'),
array('id'=>36, 'pid'=>3, 'text'=>'Bang Bang'),
array('id'=>37, 'pid'=>4, 'text'=>'君が好きだと叫びたい'),
array('id'=>38, 'pid'=>3, 'text'=>'Same Old Love'),
array('id'=>39, 'pid'=>4, 'text'=>'君をのせて'),
array('id'=>40, 'pid'=>1, 'text'=>'恋恋风尘')
);
$result=array(); foreach($list as $key=>$value){
if($value['pid']==$pid){
$result[]=$value;
}
} echo json_encode($result); // $arr=array();
// $arr[]='a';
// $arr[]='b';
?>

Angular——单页面实例的更多相关文章
- Angular——单页面与路由的使用
单页面 SPA(Single Page Application)指的是通单一页面展示所有功能,通过Ajax动态获取数据然后进行实时渲染,结合CSS3动画模仿原生App交互,然后再进行打包(使用工具把W ...
- 解决angular单页面页面底部跳转到新页面滚动条不在顶部的问题
以上jquery,下面js this.router.events.subscribe((event) => { document.body.scrollTop=0; }); 另一种写法 impo ...
- phantomjs和angular-seo-server实现angular单页面seo
1.下载phantomjs,并配置环境变量为 eg:E:\phantomjs-2.1.1-windows\bin 2.下载angular-seo-server 3.windows下:cmd eg: ...
- webpack+react+antd 单页面应用实例
React框架已经火了好长一段时间了,再不学就out了! 对React还没有了解的同学可以看看我之前的一篇文章,可以快速简单的认识一下React.React入门最好的实例-TodoList 自己从开始 ...
- 基于angular的route实现单页面cnodejs
Angular ui-router 前言 之前不太理解前端怎么实现路由功能,以前知道有一种方式使用html5的pushState可以操作url才实现路由的功能,在实践项目中也用过一次,后来这种操作叫成 ...
- 浅谈HTML5单页面架构(一)——requirejs + angular + angular-route
心血来潮,打算结合实际开发的经验,浅谈一下HTML5单页面App或网页的架构. 众所周知,现在移动Webapp越来越多,例如天猫.京东.国美这些都是很好的例子.而在Webapp中,又要数单页面架构体验 ...
- jQuery File Upload 单页面多实例的实现
jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...
- angular(3)服务 --注入---自定义模块--单页面应用
ng内部,一旦发生值改变操作,如$scope.m=x,就会自动轮询$digest队列,触发指定的$watch,调用其回调函数,然后修改dom树. 干货:https://github.com/xufei ...
- MVC route 和 Angular router 单页面的一些方式
直接看代码和注释吧 ASP.NET MVC router public class RouteConfig { public static void RegisterRoutes(RouteColle ...
随机推荐
- PromiseKit入门
原文:Getting Started with PromiseKit 作者:Michael Katz 译者:kmyhy 异步编程真的让人头疼.不管你怎样小心,总是easy出现臃肿的托付.混乱的完毕句柄 ...
- String类的四个默认成员函数
优化版的拷贝构造函数,先创建一个暂时实例tmp,接着把tmp._ptr和this->_ptr交换,因为tmp是一个局部变量.程序执行到该函数作用域外,就会自己主动调用析构函数.释放tmp._pt ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- simple-framework(MaliSDK框架分析)
simple-framework(Mali SDK框架分析) 1.全部的定义及实现放在同一个命名空间中.文件包括#include一般在命名空间的外面. 总的命名空间为namespace MaliSDK ...
- 第一天,Robert和Sue大师培训给的启示
程序猿的零点从他睡觉那一刻开始计时. 今天是周六,听到了Robert关于销售技巧的培训还有Sue关于微信零售业O2O电商的分析,一并加上昨天晚上直到11点的Leadership培训,这个周末真的是收获 ...
- 关于The hierarchy of the type TestBeforeAdvice is inconsistent的问题
今天准备写一个spring aop的demo,创建了TestBeforeAdvice类,该类实现了MethodBeforeAdvice接口,eclipse报了"The hierarchy o ...
- 用C语言实现websocket服务器
Websocket Echo Server Demo 背景 嵌入式设备的应用开发大都依靠C语言来完成,我去研究如何用c语言实现websocket服务器也是为了在嵌入式设备中实现一个ip camera的 ...
- 【POJ 3233】Matrix Power Series
[题目链接] 点击打开链接 [算法] 要求 A^1 + A^2 + A^3 + ... + A^k 考虑通过二分来计算这个式子 : 令f(k) = A^1 + A^2 + A ^ 3 + ... + ...
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- JSP-Runoob:JSP 结构
ylbtech-JSP-Runoob:JSP 结构 1.返回顶部 1. JSP 结构 网络服务器需要一个 JSP 引擎,也就是一个容器来处理 JSP 页面.容器负责截获对 JSP 页面的请求.本教程使 ...