[英]Getting Variables from URL in AngularJS
I see this documentation: http://docs.angularjs.org/api/ng.directive:ngHref 我看到以下文档: http : //docs.angularjs.org/api/ng.directive : ngHref
And this clearly shows that you can manipulate the URL based on values in an input. 这清楚地表明您可以根据输入中的值来操纵URL。
But I want to do the opposite. 但是我想相反。 I want to have
http://www.example.com/5
and feed the 5
to a variable I can use in my controller. 我想拥有
http://www.example.com/5
,并将5
馈入我可以在控制器中使用的变量。
How do I do that? 我怎么做?
Use $routeParams service to get the ID. 使用$ routeParams服务获取ID。
see as an example 举个例子
angular.module('myApp', []).config(function ($routeProvider, $routeParams) {
$routeProvider.when('/:id', {templateUrl: 'example.html', controller: 'exampleCtrl'});
});
And to get the id as a variable user $routeParams in your controller. 并在控制器中以变量用户$ routeParams的身份获取ID。
myApp.controller('exampleCtrl', function($scope, $routeParams) {
alert($routeParams.id);
});
I would call a service from my controller, and then assign scope based on the output of that function. 我会从控制器中调用服务,然后根据该函数的输出分配作用域。
If you already have the values for {chapterId:1, sectionId:2, search:'moby'}, you can either assign them to a shared scope, or cache them in your service, and retrieve them when you call your other controller 如果您已经具有{chapterId:1,sectionId:2,search:'moby'}的值,则可以将它们分配给共享作用域,或将它们缓存在服务中,并在调用其他控制器时检索它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.