java动态url_使用url Param的动态主题

快速简单的答案是使用$ rootScope . $ rootScope可供所有控制器使用,但是应该谨慎使用所有全局变量 .

基本上,如果你在你的两个控制器上使用它来寻找页面x和y

app.controller('login',['$scope','$rootScope', function ($scope,$rootScope) {

$rootScope.foo='bar';

}]);

app.controller('xCtrl',['$scope','$rootScope', function ($scope,$rootScope) {

$scope.lorem=$rootScope.foo;

console.log($scope.lorem); //bar

}]);

app.controller('yCtrl',['$scope','$rootScope', function ($scope,$rootScope) {

$scope.ipsum=$rootScope.foo;

console.log($scope.ipsum); //bar

}]);

然后可以像往常一样在HTML标记中使用它们 .

使用$ rootScope要简单得多,但是如果你想每次都使用路由器来下载id,那么它也是可能的 .

app.config(function($routeProvider){

$routeProvider

.when('/',{

controller: 'indexController',

templateUrl: 'view/index.html'

})

.when('/:id/about',{

controller: 'xCtrl',

templateUrl: 'view/x.html'

})

.otherwise({

redirectTo: '/'

});

});

app.controller('xCtrl',['$scope','$routeParams', function($scope,$routeParams){

$scope.foo=$routeParams.id;

}]);

如果你有更多的页面而不仅仅是/我可以想象这对路由有点棘手 .

您甚至可以将其与根范围耦合,然后将其传递给其他控制器 .

app.controller('xCtrl',['$scope','$rootScope','$routeParams', function($scope,$rootScope,$routeParams){

$rootScope.foo=$routeParams.id;

$scope.lorem=$rootScope.foo;

}]);

--EDIT基于评论 - 我可能需要一些代码来确定你所追求的是什么,但也许这澄清了?

网址:mysite.com/blue/about

app.config(function($routeProvider){

$routeProvider

.when('/',{

controller: 'indexController',

templateUrl: 'view/index.html'

})

.when('/:id/about',{

controller: 'xCtrl',

templateUrl: 'view/x.html'

})

.otherwise({

redirectTo: '/'

});

});

app.controller('xCtrl',['$scope','$routeParams', function($scope,$routeParams){

$scope.theme=$routeParams.id;

}]);

HTML

My theme is {{theme}}.

CSS

.blue

{

background-color: blue;

}

.green

{

background-color: green;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值