【发布时间】:2013-06-18 19:41:20
【问题描述】:
我正在尝试从 AngularJS 指令中更新 $location.path。它没有按我的预期工作,即更新浏览器窗口中显示的 URL 并在我的页面上显示不同的内容。正如控制台中所见,我的函数肯定会被正确的值调用。我认为$location.path 可能会产生一些效果,因为当我单击链接时,会调用一些额外的函数,这些函数将在新页面上调用,只有新页面永远不会显示。当我在控制器中使用 $location.path 时,它按预期工作,但在我的指令中却没有。
这是我的指令的 HTML:
<div detail-link="/comments?post={{post.postId}}" ng-click="clickDetailLink()"></div>
这是指令的定义(在 CoffeeScript 中):
directives.directive 'detailLink', ['$location', ($location) ->
return (scope, element, attrs) ->
scope.clickDetailLink = ->
console.log "loading " + scope.detailLinkHref
$location.path scope.detailLinkHref
attrs.$observe 'detailLink', (value) ->
console.log "set detail link to " + value
scope.detailLinkHref = value
]
我知道这里发生了什么...我的$routeProvider 设置为处理/comments,但在这里我试图将查询字符串附加到路由。如果我使用常规的<a href="#/comments?post={{post.postId}}">,那可以正常工作,但是通过$location.path 以某种方式设置相同的路线是行不通的。这与此代码是在控制器还是指令中无关。
【问题讨论】:
标签: angularjs angularjs-directive