angularjs-如何从URL检索数据 [英] angularjs - how to retrieve data from url

查看:60
本文介绍了angularjs-如何从URL检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要使用 angularjs 从URL获取数据.例如:

I have a scenario that I need to get data from URL with angularjs. For example:

example.com/sampleurl?hk=123

我想获取变量 hk 的数据,该数据为 123 ,并在我的控制器中使用它.我该如何实现?到目前为止,我尝试使用googleed angularjs http get ,但似乎没有任何参考.

I want to get data of variable hk which is 123 and use it in my controller. How can I achieve that? So far I tried googled angularjs http get but don't seems to get any reference.

推荐答案

如果您使用ui-router,则可以这样做

If you use ui-router, you can do like this

.state('example', {
  url: 'example/:hk',
  views: {
    'content@': {
      templateUrl: 'example.html',
      controller: 'ExampleController'
    }
  }
});

并通过注入$ stateParams来返回ExampleController中的参数

And get back the parameter in your ExampleController by injecting $stateParams

angular.module('app').controller('ExampleController', function($stateParams) {
  let hk = $stateParams.hk;
};

例如,当您到达youapp.com/example/aValue时,ExampleController中的hk等于'aValue'

So for example, when you will reach youapp.com/example/aValue, hk in the ExampleController will be equal to 'aValue'

这篇关于angularjs-如何从URL检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆