SammyJS with AngularJS

2019-07-06 16:26发布

I have problems using Sammy with Angular.

I have this application module

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

And this service

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

I need this setTimeout because I don't know when $rootScope is accessible. When I put low interval number in setTimeout, event doesn't broadcasted. One second interval works OK, but I don't want such ugly solutions. How can I be sure that $rootScope is ready and I can launch url handler function?

1条回答
仙女界的扛把子
2楼-- · 2019-07-06 17:04

Do you really 'need' to use Sammy

Have you tried using Angular's routing to get the parameters?

see

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

and

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

cheers Stu

查看更多
登录 后发表回答