* Home | About | Next page * *
* Home | About | Next page * ** * Home * * * @param {string} ui-sref 'stateName' can be any valid absolute or relative state * @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#go $state.go()} */ $StateRefDirective.$inject = ['$state', '$timeout']; function $StateRefDirective($state, $timeout) { var allowedOptions = ['location', 'inherit', 'reload']; return { restrict: 'A', require: ['?^uiSrefActive', '?^uiSrefActiveEq'], link: function(scope, element, attrs, uiSrefActive) { var ref = parseStateRef(attrs.uiSref, $state.current.name); var params = null, url = null, base = stateContext(element) || $state.$current; var newHref = null, isAnchor = element.prop("tagName") === "A"; var isForm = element[0].nodeName === "FORM"; var attr = isForm ? "action" : "href", nav = true; var options = { relative: base, inherit: true }; var optionsOverride = scope.$eval(attrs.uiSrefOpts) || {}; angular.forEach(allowedOptions, function(option) { if (option in optionsOverride) { options[option] = optionsOverride[option]; } }); var update = function(newVal) { if (newVal) params = angular.copy(newVal); if (!nav) return; newHref = $state.href(ref.state, params, options); var activeDirective = uiSrefActive[1] || uiSrefActive[0]; if (activeDirective) { activeDirective.$$setStateInfo(ref.state, params); } if (newHref === null) { nav = false; return false; } attrs.$set(attr, newHref); }; if (ref.paramExpr) { scope.$watch(ref.paramExpr, function(newVal, oldVal) { if (newVal !== params) update(newVal); }, true); params = angular.copy(scope.$eval(ref.paramExpr)); } update(); if (isForm) return; element.bind("click", function(e) { var button = e.which || e.button; if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) { // HACK: This is to allow ng-clicks to be processed before the transition is initiated: var transition = $timeout(function() { $state.go(ref.state, params, options); }); e.preventDefault(); // if the state has no URL, ignore one preventDefault from the directive. var ignorePreventDefaultCount = isAnchor && !newHref ? 1: 0; e.preventDefault = function() { if (ignorePreventDefaultCount-- <= 0) $timeout.cancel(transition); }; } }); } }; } /** * @ngdoc directive * @name ui.router.state.directive:ui-sref-active * * @requires ui.router.state.$state * @requires ui.router.state.$stateParams * @requires $interpolate * * @restrict A * * @description * A directive working alongside ui-sref to add classes to an element when the * related ui-sref directive's state is active, and removing them when it is inactive. * The primary use-case is to simplify the special appearance of navigation menus * relying on `ui-sref`, by having the "active" state's menu button appear different, * distinguishing it from the inactive menu items. * * ui-sref-active can live on the same element as ui-sref or on a parent element. The first * ui-sref-active found at the same level or above the ui-sref will be used. * * Will activate when the ui-sref's target state or any child state is active. If you * need to activate only when the ui-sref target state is active and *not* any of * it's children, then you will use * {@link ui.router.state.directive:ui-sref-active-eq ui-sref-active-eq} * * @example * Given the following template: *
*
*
*