Comments (3)
<!DOCTYPE html>
<html ng-app="wsscat">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<header>wsscat的应用</header>
<div ui-view></div>
</body>
<style></style>
<script type="text/javascript" src="../js/angular.js" ></script>
<script type="text/javascript" src="angular-ui-router.js"></script>
<script>
/* 使用ui-router来进行路由定义,需要注入ui.router模块 */
var app = angular.module('wsscat', ['ui.router']);
/* 注入$stateProvider,$urlRouterProvider */
app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
/* 使用when来对一些不合法的路由进行重定向 */
$urlRouterProvider.when('', '/main');
/* 通过$stateProvider的state()函数来进行路由定义 */
$stateProvider.state('main', {
url: '/main',
templateUrl: 'views/main.html',
controller: 'mainCtrl'
}).state('main.a', {
url: '/pageMain1',
templateUrl: 'views/pageMain1.html',
controller: 'pageMain1Ctrl'
}).state('main.b', {
url: '/pageMain2/:id',
templateUrl: 'views/pageMain2.html',
controller: 'pageMain2Ctrl'
})
}]);
//路由地址为#/main
app.controller('mainCtrl', function($scope) {
$scope.name = 'wsscat';
})
//路由地址为#/main/pageMain1
app.controller('pageMain1Ctrl',function($scope){
$scope.name = 'abc'
})
app.controller('pageMain2Ctrl',function($scope,$state){
$scope.name = 'cbd'
console.log($state.params);
})
</script>
</html>
from angular-tutorial.
切换页面的时候,链接变色
首先我们用app.run方法在每次我们进入到对应路由前获取我们当前路由的地址
这里用到$location服务获取路由地址,注意我这里是用了ui-router,所以是$stateChangeSuccess,如果是ng-router那就是$routerChangeSuccess
app.run(function($rootScope, $location) {
$rootScope.$on('$stateChangeSuccess',
function(data) {
console.log($location.path())
$rootScope.path = "#"+$location.path()
})
})
然后在对应的DOM结构中写上这样来切换这个类
<a ng-href="#/main/pageMain1" class="{{'#/main/pageMain1'==path?'red':''}}">pageMain1</a>
from angular-tutorial.
ng路由
加载路由依赖,放在angular框架后面
<script type="text/javascript" src="../js/angular.js"></script>
<script type="text/javascript" src="../js/angular-route.js"></script>
$location.path
$routeProvider
两个核心方法when()
和otherwise()
- controller //function或string类型。在当前模板上执行的controller函数,生成新的scope
- controllerAs //string类型,为controller指定别名
- template //string或function类型,视图所用的模板,这部分内容将被ngView引用
- templateUrl //string或function类型,当视图模板为单独的html文件或是使用了<script type="text/ng-template">定义模板时使用
- resolve //指定当前controller所依赖的其他模块
- redirectTo //重定向的地址
from angular-tutorial.
Related Issues (20)
- angular cdn
- $cacheFactory
- ionic总结 HOT 1
- ng-cookie
- ng-repeat绑定事件和嵌套
- ng-repeat嵌套
- service demo HOT 2
- Angular API文档
- directive demo HOT 1
- angular面试题 HOT 3
- AngularJS开发人员最常犯的10个错误
- 求指教阿!! HOT 1
- Angular中的MVC模型
- Angular的post请求后台接受不了数据的解决方法
- angular2
- 引入多个angular.js时候,angular程序会出错
- Angular指令(directive)中的controller,compile,link函数有什么不同?
- angular4
- angular4+webpack
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-tutorial.