AngularJS - 基于URL更新模型 [英] AngularJS - Update model based on URL
问题描述
这基本上是一个跟进的问题这个问题:<一href=\"http://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl\">AngularJS - 如何在产生templateUrl使用$ routeParams 的?看到我的code有没有这个问题的适当的上下文。
所有的导航部分是伟大的工作,但现在,当我浏览,我想在我的 $范围
根据 primaryNav更新一些性质
和 secondaryNav
。我想我可以做 $观看
的东西,但我似乎无法得到任何工作。下面是我试了几件事情:
$ $范围手表($ location.path()函数(){...})。 //导致JS错误
$ $范围手表($位置,功能(){...})。 //函数在页面加载运行一次,而不是之后
。$ $范围手表($ window.location的,函数(){...}); //函数在页面加载运行一次,而不是之后
$ $范围腕表(window.location的,函数(){...})。 //函数在页面加载运行一次,而不是之后
$ $范围腕表(window.location.href,函数(){...})。 //导致JS错误
我大概可以创造我的控制器中的一些方法,将采取这些资产净值和更新的一切,导航关闭,只是一个 NG-点击
添加到所有的锚标签。不过,有一件事我真的很喜欢约AngularJS当时正在使用真正的URL前瞻性值的HREF(如&LT; A HREF =#/ priNav / secNav&GT;美孚&LT; / A&GT;
)。是没可能的基础上不断变化的URL来更新我的模型时,无需通过控制器上的一些方法去我的路线?我希望这是有道理的,我问。
$范围。$表(函数(){
返回$ location.path();
},函数(){
...
});
你应该通过一个函数或能的范围内来评价一个字符串
This is basically a follow up question this question: AngularJS - How to use $routeParams in generating the templateUrl?. See my code there for the proper context of this question.
All the navigation part is working great, but now when I navigate, I want to update some properties in my $scope
based on the primaryNav
and secondaryNav
. I thought I could do something with $watch
but I can't seem to get anything to work. Here are a few things I tried:
$scope.$watch($location.path(), function() {...}); //result in js error
$scope.$watch($location, function() {...}); //function runs once on page load, but not after that
$scope.$watch($window.location, function() {...}); //function runs once on page load, but not after that
$scope.$watch(window.location, function() {...}); //function runs once on page load, but not after that
$scope.$watch(window.location.href, function() {...}); //results in js error
I could probably create some method in my controller that would take these navs and update everything and navigate off and just add a ng-click
to all the anchor tags. However, one thing I really liked about AngularJS was being to use real URL-looking values for hrefs (e.g. <a href="#/priNav/secNav">Foo</a>
). Is it not possible to update my model based on the changing URL when I route without going through some method on the controller? I hope it makes sense what I'm asking.
$scope.$watch(function() {
return $location.path();
}, function(){
...
});
you should pass a function or a string that can be evaluated inside of the scope
这篇关于AngularJS - 基于URL更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!