Angular UI-Router dynamic routing based on slug from API Ajax Call. Load view based on slug

后端 未结 1 1301
说谎
说谎 2020-12-09 22:13

Examples slugs in server database accessible through API:

{slug: \"john-smith\",type: \"user\"}
{slug: \"microsoft-technologies\",type: \"company\"}
<         


        
相关标签:
1条回答
  • 2020-12-09 22:16

    There is a working plunker.

    It comes from similar issue: AngularJS ui-router - two identical route groups

    In case, I do understand your aim properly, this would be the adjusted state definition (I just skipped the $http and server response part, just working with passed parameter):

    .state('hybrid', {
        // /john-smith
        url: '/{slug:(?:john|user|company)}',
        templateProvider: ['type', '$templateRequest',
          function(type, templateRequest) 
          {
            var tplName = "tpl.partial-" + type + ".html";
            return templateRequest(tplName);
          }
        ],
        controllerProvider: ['type',
          function(type) 
          {
            return type + 'Controller';
          }
        ],
        resolve: {
          type: ['$http', '$stateParams',
            function($http, $stateParams) {
              /*$http.get({
                method: "GET",
                url: "http://localhost/api/" + $stateParams.slug
            }).success(function(response, status, headers, config){
                //response = {slug: "john-smith",type: "user"}
                return response.type
            })*/
              return $stateParams.slug
            }
          ]
        }
    })
    

    One change is the resolove : {} became: resolve : {}. Another is fixture of the controller def (rt vs type). And also, we do profit from built in features templateProvider and $templateRequest (similar here: Angular ui.router reload parent templateProvider)

    check that in action here

    EXTEND, including the $http part (extended plunker)

    Let's adjust (for plunker purposes) the server part to return this info as data.json:

    {
     "john-smith": {"type": "user"},
     "lady-ann": {"type": "user"},
     "microsoft-technologies" : {"type": "company" },
     "big-company" : {"type": "company" },
     "default": {"type" : "other" }
    }
    

    And these links:

    <a href="#/john-smith">
    <a href="#/lady-ann">
    
    <a href="#/microsoft-technologies">
    <a href="#/big-company">
    
    <a href="#/other-unknown">
    

    Will be easily managed by this adjusted state def:

      .state('hybrid', {
        // /john-smith
        url: '/:slug',
        templateProvider: ['type', '$templateRequest',
          function(type, templateRequest) 
          {
            var tplName = "tpl.partial-" + type + ".html";
            return templateRequest(tplName);
          }
        ],
        controllerProvider: ['type',
          function(type) 
          {
            return type + 'Controller';
          }
        ],
        resolve: {
          type: ['$http', '$stateParams',
            function($http, $stateParams) {
              return $http.get("data.json")
                .then(function(response){
                  var theType = response.data[$stateParams.slug]
                      ||response.data["default"]
                  return theType.type
                })
            }
          ]
        }
      })
    

    Check that updated stuff here

    0 讨论(0)
提交回复
热议问题