angularjs - Accessing ui-router state params from an attribute-type directive (NOT a component) -
we're upgrading old version of ui router v1.0.5, , i'm having issue trying replace our deprecated $stateparams uses. use simplified example, have unique-name attribute-type directive use on form field validation check make sure text in field unique name. idea we're either adding new doohickey or changing name of existing doohickey, , want ensure no other doohickey has same name.
in our old code, doohickeyid value $stateparams this:
function uniquename(doohickeysearchservice, $stateparams, $q) { return { restrict: 'a', require: 'ngmodel', link: function (scope, elm, attrs, ctrl) { ctrl.$asyncvalidators.uniquename = function (modelvalue) { // use doohickeysearchservice founddoohickey name // then... return founddoohickey.id !== $stateparams.doohickeyid; }; } }; } probably way over-simplified, core of problem there: how access doohickeyid value without using deprecated $stateparams? there's parent state that's getting url , /doohickey/{doohickeyid:int}/edit, specified in state definition.
i can use $state $current state, , array of states path property, , can see doohickeyid param in 1 of states in path. unfortunately, if take value() of param, comes undefined, though $stateparams has correct doohickeyid value. i've tried adding doohickeyid resolve on parent state, unfortunately doesn't give me data on attribute-type directive need it.
reading docs , googling turning references accessing parent data child states, or component, i'm trying access attribute-type directive, not state/component. there way without giving our code base major overhaul?
have tried solution ui-router documentation?
instead of using global $stateparams service object,
inject $uirouterglobals , use uirouterglobals.params
myservice.$inject = ['$uirouterglobals']; function myservice($uirouterglobals) { return { paramvalues: function () { return $uirouterglobals.params; } } }instead of using per-transition $stateparams object,
inject current transition (as $transition$) , use transition.params
mycontroller.$inject = ['$transition$']; function mycontroller($transition$) { var username = $transition$.params().username; // .. username }
https://ui-router.github.io/ng1/docs/latest/modules/injectables.html#_stateparams
Comments
Post a Comment