How to add guid regex in angular ui-router state

后端 未结 3 1515
庸人自扰
庸人自扰 2021-01-03 15:49

I have this route



        
3条回答
  •  失恋的感觉
    2021-01-03 16:23

    After much trial and error, I came up with:

    $urlMatcherFactoryProvider.type('guid', {
        encode: angular.identity,
        decode: angular.identity,
        equals: function(a, b) { return a.toUpperCase() === b.toUpperCase(); },
        is: function(val) {
            // Local to avoid any lastIndex issues
            var GUID_REGEXP = /^[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}$/i;
            return GUID_REGEXP.test(val);
        },
        // No anchors or flags with pattern
        pattern: /[a-fA-F\d]{8}-(?:[a-fA-F\d]{4}-){3}[a-fA-F\d]{12}/
    });
    

    This is based on @just-boris's answer. One key difference is pattern. Without this it appears that invalid GUIDs will not trigger $urlRouterProvider.otherwise().

提交回复
热议问题