【发布时间】:2016-04-06 13:30:34
【问题描述】:
我有一个 html 文件,它与 angular 中的指令一起使用,其中 scope.course 在 .js 文件中定义。
在下面的代码中,ng-href="courses/{{course.url}}" 工作得很好,并提供了“课程/myurl”的链接
但是当在 ng-class 的 isActive 方法中放入一个变量时,它不会显示,就好像我无法从这里访问它一样。
<li ng-class={active: isActive('/courses/{{course.url}}')}><a ng-href="courses/{{course.url}}">Overview</a></li>
我的 isActive 方法如下所示:
scope.isActive = function(route){
console.log(route);
return route === $location.path();
}
但它只记录“/courses/”而没有其他任何内容
如何将 course.url 放入我的 isActive 方法中?
【问题讨论】:
-
您的 isActive 方法应该返回一个布尔值。你能粘贴你的 isActive 方法代码吗?
-
我的isActive方法是:
scope.isActive = function(route){ return route === $location.path(); } -
是的,那么我相信 fikkatra 发布的答案应该有效。您是否在控制台上检查过路线 === $location.path() 是否为真?您正在使用严格的相等运算符。可能存在差异。请尝试仔细检查。
标签: javascript html angularjs angularjs-directive scope