首页 文章

需要理解Angular路由的方法

提问于
浏览
0

大家好我是angularJs的新手,我发现很难理解路由概念,我从很多来源学到了,但仍然是's not quite clear specially the when method. What i understood is routing helps us to achieve single page application , which means browser loads only one .html file and then other contents are removed or added based on user'的交互,没有其他文件被加载 . 但是在以下链接的定义中angular routing

templateUrl: When the HTML to be displayed is complex and large, it’s better to break them into separate templates/files, and give the URL to the HTML file as the templateUrl. AngularJS loads the HTML file from the server when it needs to display the particular route.

它说angularjs将HTML文件加载为templateUrl . 有人可以向我解释一下吗?

3 回答

  • -2
    1. templateUrl 属性告诉AngularJS应该使用 ngView 指令在 div 内加载和显示哪个HTML模板 .

    2.它可以是路径(或)函数,而.when()方法将路径和路径作为参数 .

    3.它可以用作返回生成的URL的函数 . 我们可以通过传递参数来操纵url .

    .when('/:screenName/getAll',{
        templateUrl: function(route){
             return route.screenName +'/getAllList'
        }
    })
    

    所以,它不像重新加载整个html页面 . 只有html内容加载到 div 内..希望这有帮助 . !!!

  • 0

    例如:

    //main.js
    $routeProvider.when('/computers', {
        templateUrl: 'views/computers.html',
    });
    
    
    //./views/computers.html
    <ul>
        <li>Computer 1</li>
        <li>Computer 2</li>
    </ul>
    

    当路由为 http://example.com/#/computers 时,AngularJs将通过ajax加载 ./views/computers.html 文件,并将其缓存以用于其他请求 . 因此,它不仅会在您的SPA中加载一个(主要的)html文件 .

  • -1

    Angular加载html文件作为模板url意味着它使用您提供的路径来加载html . 与在角度组件中编写html相比,这将更有条理 .

    单页应用程序并不意味着只加载一个文件的角度加载 . 当用户交互时,可以执行路由,将加载更多数据,并且将加载更多html或html的某些部分以及数据以呈现给用户 .

    “when”方法意味着当用户访问定义的路径时,将继续执行所有操作,包括“templateUrl” .

相关问题