Using routes in AngularJS to gather data

旧城冷巷雨未停 提交于 2020-01-06 05:47:05

问题


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

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?


回答1:


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
});


来源:https://stackoverflow.com/questions/29289957/using-routes-in-angularjs-to-gather-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!