{"id":89,"date":"2015-01-15T18:30:03","date_gmt":"2015-01-15T18:30:03","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=89"},"modified":"2015-01-15T18:30:03","modified_gmt":"2015-01-15T18:30:03","slug":"ui-router-dynamic-states-dynamically-create-states-list-states","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=89","title":{"rendered":"UI-Router Dynamic States &#8211; Dynamically create states &#8211; List States"},"content":{"rendered":"<p>Writing boiler plate code for ui-router in angularjs isn&#8217;t required.<\/p>\n<p>The code below dynamically creates states in ui-router the first time ui-router is called.<\/p>\n<p>VIEWS, LIST, CREATE, VIEW, EDIT are constants defined in app.config.js.<\/p>\n<p><code><br \/>\n\t$state.get(); \/\/ LISTS CURRENT STATES<\/p>\n<p>\t\/\/ CONFIGURE STATES<br \/>\n\tvar statesCtrls = [<br \/>\n\t\t{ state:'test', ctrl:\"TestsCtrl\" },<br \/>\n\t\t{ state:'tag', ctrl:\"TagsCtrl\" },<br \/>\n\t\t{ state:'tagType', ctrl:\"TagTypesCtrl\" }<br \/>\n\t\t];<\/p>\n<p>\tfunction createState(stateCtrl){<br \/>\n\t\tvar ctrl = stateCtrl.ctrl;<br \/>\n\t\tvar state = stateCtrl.state;<br \/>\n\t\tvar list = {<br \/>\n\t\t\turl: \"\/\" + state ,<br \/>\n\t\t\ttemplateUrl: VIEWS + state + \"-\" + LIST + \".html\",<br \/>\n\t\t\tcontroller: ctrl<br \/>\n\t\t};<\/p>\n<p>\t\tvar create = {<br \/>\n        \turl: \"\/\" + state + \"\/\" + CREATE,<br \/>\n\t\t\ttemplateUrl: VIEWS + state + \"-\" + CREATE + \".html\",<br \/>\n\t\t\tcontroller: ctrl<br \/>\n    \t};<\/p>\n<p>    \tvar view = {<br \/>\n        \turl: \"\/\" + state + \"\/:id\",<br \/>\n\t\t\ttemplateUrl: VIEWS + state + \"-\" + VIEW + \".html\",<br \/>\n\t\t\tcontroller: ctrl<br \/>\n    \t};<\/p>\n<p>\t\tvar edit = {<br \/>\n        \turl: \"\/\" + state + \"\/\" + EDIT + \"\/:id\",<br \/>\n\t\t\ttemplateUrl: VIEWS + state + \"-\" + EDIT + \".html\",<br \/>\n\t\t\tcontroller: ctrl<br \/>\n    \t};<\/p>\n<p>        $stateProvider.state(state, list);<br \/>\n        $stateProvider.state(state + \"\/\" + CREATE, create);<br \/>\n        $stateProvider.state(state + \"\/\", view);<br \/>\n        $stateProvider.state(state + \"\/\" + EDIT + \"\/\", edit);<br \/>\n\t}<\/p>\n<p>    angular.forEach(statesCtrls, function (stateCtrl) {<br \/>\n    \tcreateState(stateCtrl);<br \/>\n    });<\/p>\n<p>    $state.get(); \/\/ RELISTS CURRENT STATES<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing boiler plate code for ui-router in angularjs isn&#8217;t required. The code below dynamically creates states in ui-router the first time ui-router is called. VIEWS, LIST, CREATE, VIEW, EDIT are constants defined in app.config.js. $state.get(); \/\/ LISTS CURRENT STATES \/\/ CONFIGURE STATES var statesCtrls = [ { state:&#8217;test&#8217;, ctrl:&#8221;TestsCtrl&#8221; }, { state:&#8217;tag&#8217;, ctrl:&#8221;TagsCtrl&#8221; }, { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":3,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}