在AngularJS中使用路由来收集数据 [英] Using routes in AngularJS to gather data

查看:115
本文介绍了在AngularJS中使用路由来收集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于AngularJS构建的单页面应用程序,该应用程序是使用HTML5路由配置的。
所以我用:

I have an single page app built on AngularJS which is configured using HTML5 routing. So I use:

http://www.example.com/products rather than http://www.example.com/#/products

我也有联盟会员可以使用的通配符子域,例如:

I also have wildcard subdomains affiliates can use for example:

http://myaffiliate.example.com

然后我使用此控制器从火力基地收集有关myaffiliate的数据:

And I gather data about myaffiliate from a firebase using this controller:

app.controller("ReplicatedController", function($scope, $firebaseObject) {

    var parts = location.hostname.split('.');
    var refSubdomain = parts.shift();

    var ref = new Firebase("https://example-firebase.firebaseio.com/" + refSubdomain);
    var syncObject = $firebaseObject(ref);
    syncObject.$bindTo($scope, "coach");
});

这一切都很好,但是除了使用通配符子域外,我还需要会员才能使用网址。例如:

This all works fine but in addition to using a wildcard subdomain I also need affiliates to be able to use urls. For example:

http://example.com/myaffiliate

有可能做到这一点,我该怎么做?

Is it possible to do this, and how do I do that?

推荐答案

您将需要$ routeProvider服务,以便您可以发送路由参数。这样您就可以执行以下操作。

You're going to need the $routeProvider service so you can send route parameters. Then you're able to do something like this.

.config(function($routeProvider){
    $routeProvider.when('/:my_affiliate', {
        //other route details
    });
})

.controller('Ctrl', function($scope, $routeParams) {
  console.log($routeParams.my_affiliate); // This will print affiliate
});

这篇关于在AngularJS中使用路由来收集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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