使用 AngularJS 中的路由收集数据

Using routes in AngularJS to gather data

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

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

我也有通配符子域可以使用例如:

http://myaffiliate.example.com

我使用此控制器从 firebase 收集有关 myaffiliate 的数据:

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

这一切都很好,但除了使用通配符子域之外,我还需要附属机构才能使用 url。例如:

http://example.com/myaffiliate

是否可以这样做,我该怎么做?

您将需要 $routeProvider 服务以便发送路由参数。然后你就可以做这样的事情了。

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

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