加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 默认情况下,在ui.router中更改$state.go()的默认行

发布时间:2020-12-17 07:55:12 所属栏目:安全 来源:网络整理
导读:我有一个使用ui.router包构建的Angular应用程序用于URL路由.我想更改它,以便如果用户尝试导航到他们已经在的页面,路由器将重新加载该状态而不是什么都不做.每个 http://angular-ui.github.io/ui-router/site/#/api/ui.router.state. $state#go,$state.go确实
我有一个使用ui.router包构建的Angular应用程序用于URL路由.我想更改它,以便如果用户尝试导航到他们已经在的页面,路由器将重新加载该状态而不是什么都不做.每个 http://angular-ui.github.io/ui-router/site/#/api/ui.router.state. $state#go,$state.go确实采用了一个重新加载参数,但它默认为false.现在,在我的代码中重写每个调用$state.go和每个ui-sref以将reload设置为true的想法都是一个糟糕的想法.我想要的是为$state.go改变该参数的默认值的一些方法,或者至少是ui-sref装饰器.

在http://angular-tips.com/blog/2013/09/experiment-decorating-directives/之后,我试图至少扩展ui-sref指令(因为除了显式的$state.go调用之外还有更多的指令)

myApp.config(function($provide) {
    // override ui-sref to have the reload option default to true.
    $provide.decorator('uiSrefDirective',function($delegate){
        var directive = $delegate[0];
        var link = directive.link;

        directive.compile = function() {
            return function(scope,element,attrs) {
                if(attrs.uiSrefOpts == null){
                    attrs.uiSrefOpts = {};
                }

                if(attrs.uiSrefOpts.reload == null){
                    attrs.uiSrefOpts.reload = true;
                }
                console.log(arguments);

                link.apply(this,arguments);
            };
        };
        return $delegate;
    });

然而,这实际上似乎没有任何成就,即使它确实如此,它实际上也不会影响$state.go.有没有人有任何想法如何在ui.router代码中手动更改这种行为?

我在下面调整了你的方法.我们将从装饰中获益,但不会从指令中获益.我们可以在这里查看,在 ui-sref directive源代码中,click事件实际上调用了:
$state.go(ref.state,params,options);

因此,我们可以装饰$state提供程序,这可能非常简单:

.config(function ($provide) {
    $provide.decorator('$state',function ($delegate) {

        // let's locally use 'state' name
        var state = $delegate;

        // let's extend this object with new function 
        // 'baseGo',which in fact,will keep the reference
        // to the original 'go' function
        state.baseGo = state.go;

        // here comes our new 'go' decoration
        var go = function (to,options) {
            options = options || {};

            // only in case of missing 'reload'
            // append our explicit 'true'
            if (angular.isUndefined(options.reload)) {

                options.reload = true;
            }

            // return processing to the 'baseGo' - original
            this.baseGo(to,options);
        };

        // assign new 'go',right now decorating the old 'go'
        state.go = go;

        return $delegate;
    });        
})

这就够了.现在任何状态更改(包括单击ui-sref)都会触发重新加载.

注意:只是必须说,我不认为这是这样的.触发重装一直……对我来说,似乎我们实际上松了很多,松了钻石的优点 – ui-router

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读