How to prevent redirecting in Angular Js1.2

后端 未结 9 2008
情歌与酒
情歌与酒 2020-12-24 02:09

I was using AngularJs-1.0.7 and Bootstrap in my application. Recently I migrated from AngularJs-1.0.7 to AngularJs-1.2. I am using Bootstrap\'s Accordions and Tabs.

9条回答
  •  不思量自难忘°
    2020-12-24 02:51

    Your solution in your question does work, but to prevent having to check for each anchor's id, change

    if (attrs.href === '#firstTab'|| attrs.href === '#secondTab')
    

    to

    if (attrs.href && attrs.href.indexOf('#') > -1)
    

    Directive:

    .directive('a', function () {
        return {
            restrict: 'E',
            link: function (scope, elem, attrs) {
                if (attrs.href && attrs.href.indexOf('#') > -1) {
                    elem.on('click', function (e) {
                        e.preventDefault();
                    });
                }
            }
        };
    })  
    

提交回复
热议问题