码迷,mamicode.com
首页 > Web开发 > 详细

AngularJs(3)

时间:2017-06-07 21:43:52      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:ngroute $routeprovider

<!doctype html>

<html lang="en" ng-app=‘myApp‘ >

<head>

<meta charset="UTF-8">

<title>路由一</title>

<script type="text/javascript" src="angular.min.js"></script>

<script type="text/javascript" src="angular-route.min.js"></script>

<script type="text/javascript">


//在模块中的[]中引入ngRoute

var myApp = angular.module(‘myApp‘, [‘ngRoute‘])

//在配置中引入$routeProvider

myApp.config([‘$routeProvider‘,function($routeProvider){

$routeProvider

//根据哈希值确定ng-view视图的内容

//:num获取传递过来的参数

.when(‘/aaa/:num‘,{

template : ‘<p>首页的内容</p>{{name}}‘,

controller : ‘one‘

})

.when(‘/bbb‘,{

template : ‘<p>分页一的内容</p>{{name}}‘,

controller : ‘two‘

})

.when(‘/ccc/:num‘,{

template : ‘<p>分页二的内容</p>{{name}}‘,

controller : ‘three‘

})

//设置默认值

.otherwise({

redirectTo :‘/aaa‘

});

}]);

 myApp.controller(‘one‘,[‘$scope‘,‘$location‘,‘$routeParams‘,function($scope,$location,$routeParams){

  $scope.name=‘hello‘;

  $scope.$location=$location;

  //可以获取传递过来的参数

  console.log($routeParams);

 }]);

 myApp.controller(‘two‘,[‘$scope‘,function($scope){

  $scope.name=‘hi‘;

  //$scope.$location=$location;

 }]);

  myApp.controller(‘three‘,[‘$scope‘,‘$routeParams‘,function($scope,$routeParams){

  $scope.name=‘你好‘;

  //$scope.$location=$location;

  console.log($routeParams);

 }]);

</script>

</head>

<body ng-controller=‘one‘>

<a href="" ng-click=‘$location.path("aaa/123")‘>首页</a>

<a href="" ng-click=‘$location.path("bbb")‘>分页一</a>

<a href=""  ng-click=‘$location.path("ccc/333")‘>分页二</a>

<div ng-view></div>

</body>

</html>



本文出自 “12897581” 博客,请务必保留此出处http://12907581.blog.51cto.com/12897581/1933205

AngularJs(3)

标签:ngroute $routeprovider

原文地址:http://12907581.blog.51cto.com/12897581/1933205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!