AngularJs路由

来自ling
跳转至: 导航搜索

常用代码

$state.go("marketBeian",{},{reload:true}); 
$location.url('eit/legal/legalDetail?legalInfoId='+data.id);
location.href = '#/eit/legal/legalDetail?1=2'
$state.go('eit.legalDetail', {legalInfoId: data.id},{reload:true});
// 带#号的url,看?号的url,见下面  
url = http://qiaole.sinaapp.com?#name=cccccc  
  
$location.absUrl();  
// http://qiaole.sinaapp.com?#name=cccccc  
  
$location.host();  
// qiaole.sinaapp.com  
  
$location.port();  
// 80  
  
$location.protocol();  
// http  
  
$location.url();  
// ?#name=cccccc  
  
// 获取url参数  
$location.search().name;  
// or  
$location.search()['name'];  
  
// 注:如果是这样的地址:http://qiaole.sinaapp.com?name=cccccc  
  
var searchApp = angular.module('searchApp', []);  
searchApp.config(['$locationProvider', function($locationProvider) {  
  $locationProvider.html5Mode(true);  
}]);  
searchApp.controller('MainCtrl', ['$scope', '$location', function($scope, $location) {  
  if ($location.search().keyword) {  
    $scope.keyword = $location.search().keyword;  
  }  
}]);

或设置

           .state('marketBeian', {
               url: '/market/beian',
               templateUrl: 'views/market/beian.html',
               cache:false,
               controller: 'MarketBeianCtrl',
               data: {
                   title: '作业备案信息管理'
               }
           })     

分开路由

angular.module('main', ['main.page1']).config(function($stateProvider) {
    $stateProvider.state('main', {...})
});

angular.module('main.page1', []).config(function($stateProvider) {
    $stateProvider.state('main.page1', {...})
});


var myApp = angular.module('myApp', ['ngRoute']);
    myApp.controller('TextController', function ($scope) {
      $scope.someText = '测试显示内容';
    });
 
    //路由
    function emailRouteConfig($routeProvider) {
      $routeProvider.
      when('/', {
        controller: ListController,
        templateUrl: 'list.html'
      }).
      when('/view/:id', { //在id前面加一个冒号,从而制订了一个参数化URL
        controller: DetailController,
        templateUrl: 'detail.html'
      }).
      otherwise({
        redirectTo: '/'
      });
    }
 
    myApp.config(emailRouteConfig);//配置我们的路由
 
    messages = [{
      id: 0, sender: "123456@qq.com", subject: "你好,这是一封邮件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,这是发送给您的邮件。"
    }, {
      id: 1, sender: "123456@qq.com", subject: "你好,这是一封邮件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,这是发送给您的邮件。"
    }, {
      id: 2, sender: "123456@qq.com", subject: "你好,这是一封邮件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,这是发送给您的邮件。"
    }];
 
    function ListController($scope) {
      $scope.messages = messages;
    }
 
    function DetailController($scope,$routeParams) {
      $scope.message = messages[$routeParams.id];
    }
 <div ui-view="page" ng-show="islist">
	var url=$location.url();
	console.log(url.indexOf("#"))
	if(url.indexOf("#")<0||url.indexOf("#")>0){//不带#或不在第一个
		gopage("checkout.html#/main");
	}


app.config(["$stateProvider", "$urlRouterProvider", "$httpProvider", function ($stateProvider, $urlRouterProvider, $httpProvider) {
    $stateProvider.state('main', {
            url: "/main", views: {
                "page": {
                    templateUrl: "checkout_main.html"
                }
            }
        }).state('selectaddress', {
            url: "/selectaddress", views: {
                "page": {
                    templateUrl: "checkout_selectaddress.html"
                }
            }
        }).state('editaddress', {
            url: "/editaddress", views: {
                "page": {
                    templateUrl: "checkout_editaddress.html"
                }
            }
        }).state('changeregion', {
            url: "/changeregion", views: {
                "page": {
                    templateUrl: "checkout_changeregion.html"
                }
            }
        })
//       $urlRouterProvider.when("/main", "main");
       //$urlRouterProvider.otherwise("/main");
}]).run(function($rootScope) {
	$rootScope.ecommerce={
			name:"XXXXXXXX"
	}
	var nickName=getStorage(NIKINAME);
	$rootScope.isLogin = false;	
	$rootScope.userInfo={};
	$rootScope.copyright = "Copyright Ecommerce2016 www.ling2.cn";	
	if(nickName){
		$rootScope.isLogin=true;
		$rootScope.userInfo.nickName=nickName;
	}
});