jab*_*jab 7 angularjs angularjs-directive
我正在尝试$location.path从AngularJS指令中更新.它没有按照我的预期工作,即更新浏览器窗口中显示的URL并在我的页面上显示不同的内容.我的函数肯定会被调用正确的值,如控制台中所示.我认为$location.path可能会产生一些影响,因为当我点击链接时,会调用一些在新页面上调用的附加函数,只会显示新页面.当我$location.path在控制器中使用时,它按预期工作,但不在我的指令中.
这是我的指令的HTML:
<div detail-link="/comments?post={{post.postId}}" ng-click="clickDetailLink()"></div>
Run Code Online (Sandbox Code Playgroud)
这是指令的定义(在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
]
Run Code Online (Sandbox Code Playgroud)
我知道这里发生了什么......我$routeProvider已经设置好处理/comments但是在这里我试图将一个查询字符串附加到路由中.如果我使用常规<a href="#/comments?post={{post.postId}}">,这工作正常,但以某种方式设置相同的路线通过$location.path是行不通的.这与代码是否在控制器或指令中无关.