search

Home  >  Q&A  >  body text

angular.js - angularjs ui-router dynamically switches the view to the specified ui-view

a.html

<p ui-view='index'></p>

b.html

<p ui-view='content'></p>

c.html

<p>content1</p>

d.html

<p>content2</p>

The binding event in a.html triggers the content view to switch to c.html or d.html

How to configure the config of $stateProvider
How to manually switch in js?

phpcn_u1582phpcn_u15822839 days ago838

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-15 16:51:32

    Your question lacks some necessary contextual information, so I can only rely on speculation or guessing to complete it

    The routing system of any SPA framework is the same: each route corresponds to the status of the application, and changes in the application status are reflected in changes in URLs; conversely, changes in URLs will cause the routing system to dynamically refresh the application The status of the program.

    In your example, there is only one constant entrance to the routing ui-view='content',但却要不同的视图(c.htmld.html)动态的进入其中,这就意味着 c.htmld.html It needs to correspond to the different states of the application, so that the routing system can dynamically update it accordingly.

    Suppose your example looks like this:

    • when /posts/show/c 的时候,ui-view='content' 显示 c.html

    • when /posts/show/d 的时候,ui-view='content' 显示 d.html

    So we can correspond to ui-router:

    • stateposts.show

    • Dynamic fragments, that is, the variable parts of URLs, correspond to state 里的 url,我把它命名为 :name

    • view 的入口是 content@posts.show

    This is enough to write most of the code:

    angular.module('YourApp').config(function ($stateProvider) {
    
        $stateProvider.state('posts.show', {
            url: '/show/:name',
            views: {
                'content@posts.show': {
                    templateUrl: /* TODO */,
                    controller: ...
                }
            }
        });
    
    });

    The only question left is: when :name 变化的时候,如何更新 templateUrl?

    As mentioned before, changes in URLs will cause the routing system to update the application's state, and these changes are saved in the routing system $params 对象中,我们可以用该对象提取变化的部分并生成正确的 templateUrl. Here I write a helper function to do this:

    angular.module('YourApp').config(function ($stateProvider) {
    
        $stateProvider.state('posts.show', {
            url: '/show/:name',
            views: {
                'content@posts.show': {
                    templateUrl: getTemplateUrl,
                    controller: ...
                }
            }
        });
    
        function getTemplateUrl() {
            return: 'templates/posts/' + $params.name + '.html';
        }
    
    });

    ui-router This module templateUrl not only receives strings, but can also receive function return values. So you can get:

    • When /posts/show/c, /posts/show/c 的时候,templateUrltemplates/posts/c.html is templates/posts/c.html

    • When /posts/show/d, /posts/show/d 的时候,templateUrltemplates/posts/d.html is templates/posts/d.html

    Correspondingly, after your index 里的导航链接就要负责切换 /posts/show/:name changes, the link on how to generate the corresponding dynamic fragment is in the ui-router documentation, go find it yourself.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 16:51:32

    I think the people above are making the game too complicated. Wouldn’t it be nice to just watch a ui-view tutorial
    https://scotch.io/tutorials/a...

    reply
    0
  • Cancelreply