【问题标题】:Using routes in AngularJS to gather data在 AngularJS 中使用路由来收集数据
【发布时间】:2015-03-27 07:52:36
【问题描述】:

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

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

我也有附属的通配符子域,例如:

http://myaffiliate.example.com

我使用这个控制器从一个火力基地收集关于我的附属公司的数据:

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

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

【问题讨论】:

    标签: angularjs routing


    【解决方案1】:

    您将需要 $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
    });
    

    【讨论】:

    • 按预期工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2016-01-11
    • 2013-05-24
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多