使用URL数据更改类AngularJS [英] Changing the class in AngularJS using URL data

查看:87
本文介绍了使用URL数据更改类AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变类的元素,当我更改页面,并使用location.path()或东西来获取相关的URL片段。

I want to change the class of an element when I change the page and use location.path() or something to grab the relevant URL fragment.

我使用$ routeProvider路由。小提琴不能正常显示,但它在我的code正常工作。我遇到的问题是,当我加载下一页它不会更新。

I am using $routeProvider for routing. The fiddle doesn't properly show it, but it works fine in my code. The problem I am having is that it does not update when I load the next page.

这code拿起网址片段,我想:

This code picks up the url fragment I want:

$scope.locationPath = $location.path().replace(/^\/([^\/]*).*$/, '$1');

下面是一个非常简单的jsfiddle:
http://jsfiddle.net/timothybone/kmBXv/1/

Here is a very simple JSfiddle: http://jsfiddle.net/timothybone/kmBXv/1/

推荐答案

威尔的答案几乎是正确的;你必须包装 $ location.path()的功能,所以它的每一个消化,不只是一次执行:

will's answer is almost right; you have to wrap $location.path() in a function so it's executed on every digest, not just once:

$scope.$watch(function () {
  return $location.path();
}, function() {
  return $location.path().replace(/^\/([^\/]*).*$/, '$1');
});

工作小提琴: http://jsfiddle.net/kmBXv/3/

这篇关于使用URL数据更改类AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆