使用AngularJs可以方便的做出单页面应用,既然是应用,那就涉及到页面跳转的问题,使用Angularjs的路由插件可以方便的使用路由功能,而且可以传参非常容易。
代码如下:
路由插件:angular-ui-router.js。
文件:index.html
04 | <meta charset= "UTF-8" > |
06 | <style type = "text/css" > |
07 | td{height:30px;line-height:30px;border:1px solid |
12 | <a ui-sref= "home" >首页</a> |
13 | <a ui-sref= "list" >列表页</a> |
14 | <a ui-sref= "article" >文章页</a> |
16 | <div ng-controller= "ctr" > |
17 | <a href= "javascript:;" ng-click= "fn('hello', 'world');" >点我</a> |
22 | <script type = "text/javascript" src= "http://www.zymseo.com/js/angular.min.js" ></script> |
23 | <script type = "text/javascript" src= "http://www.zymseo.com/js/angular-ui-router.js" ></script> |
24 | <script type = "text/javascript" > |
25 | var m = angular.module( 'app' , [ 'ui.router' ]); |
26 | m.config([ '$urlRouterProvider' , '$stateProvider' , function ($urlRouterProvider, $stateProvider){ |
27 | //默认路由,没有匹配的路由时,跳转到这个地址 |
28 | $urlRouterProvider.otherwise( '/home' ); |
30 | $stateProvider.state( 'home' , { |
32 | templateUrl : function (){ |
38 | templateUrl : function (){ |
45 | params : {a: '' , b: '' }, |
46 | templateUrl : function ($routerParams){ |
47 | return './article.html?a=' +$routerParams.a+ '&b=' +$routerParams.b; |
51 | .state( 'webcontent' , { |
52 | url : '/webcontent/{id}' ,//ui-sref中的接受参数 |
53 | templateUrl : function (){ |
54 | return './web_content.html' ; |
59 | //控制器中使用到了$scope、$http、$stateParams、$state等服务 |
60 | m.controller( 'ctr' , [ '$scope' , '$http' , '$stateParams' , '$state' , function ($scope, $http, $stateParams, $state){ |
65 | }). then ( function (result){ |
66 | $scope.webList = result.data; |
67 | return $scope.webList; |
70 | obj. then ( function (result){ |
71 | for (var i=0; i<result.length; i++){ |
72 | if ($stateParams. id ==result[i].webid){ |
73 | $scope.webname = result[i].webname; |
79 | $scope.fn = function (a, b){ |
80 | $state.go( 'article' , {a:a, b:b}); |
83 | //通过$stateParams服务获取网址中的参数 |
85 | $scope.a = $stateParams.a; |
88 | $scope.b = $stateParams.b; |
|
文件:home.html
04 | <meta charset= "UTF-8" > |
|
文件:list.html
04 | <meta charset= "UTF-8" > |
08 | <table cellpadding= "0" cellspacing= "0" > |
17 | < tr ng-repeat= "(k, v) in webList" > |
19 | <td><a ui-sref= "webcontent({id:v.webid})" >{{ v .webname}}</a></td><!--ui-sref传参--> |
|
文件:article.html
04 | <meta charset= "UTF-8" > |
|
文件:web_content.html
04 | <meta charset= "UTF-8" > |
08 | <h1>这里是:{{webname}}</h1> |
|
文件:test.php
03 | header( 'Content-type:text/html;charset="utf8"' ); |
10 | echo json_encode( $arry ); |
|
以上几个文件放置到同一目录下即可!