SammyJS与AngularJS合作

时间:2013-04-10 06:13:54

标签: angularjs sammy.js

我在使用带有Angular的Sammy时遇到了问题。

我有这个应用程序模块

var appModule = angular.module('myApp', []).run(function (routes) {
    routes.run('#/');
});

这项服务

appModule.factory('routes', function ($rootScope) {

    var routes = $.sammy(function () {

        this.get('#/room/:roomId', function () {
            var that = this;
            setTimeout(function () {
                $rootScope.$broadcast('user has entered the room', {roomId: that.params['roomId']});
            }, 1000);

        });

    });

    return routes;
});

我需要这个setTimeout,因为我不知道$ rootScope何时可以访问。当我在setTimeout中设置低间隔号时,事件不会被广播。一秒间隔工作正常,但我不想要这样丑陋的解决方案。我怎么能确定$ rootScope已经准备就绪,我可以启动url处理函数吗?

1 个答案:

答案 0 :(得分:2)

你真的'需要'使用Sammy

您是否尝试过使用Angular路由获取参数?

http://docs.angularjs.org/api/ng.$route

http://docs.angularjs.org/api/ng.$routeParams

欢呼声 斯图