2016年4月15日 星期五

AngularJS範例說明8-ng-view、ngRoute、$routeProvider

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.js"></script>
    <!--1. include angular-route.js in your HTML-->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-route.js"></script>
    <title></title>
    <meta charset="utf-8" />
</head>
<body ng-app="myApp">

    <!--2. ng-view是由ngRoute提供的一個特殊指令,是routing後視圖模板的作用域-->
    <div ng-view></div>

    <script type="text/javascript">
        //3. load the module in your application
        angular.module('myApp', ['ngRoute']).
        //4. Config區塊使用$routeProvider定義routes
        config(['$routeProvider', function ($routeProvider) {
            $routeProvider
            //AngularJS提供的whenotherwise兩個方法來定義應用的路由
            /*使用when來添加一個特定路由,此方法可以添加兩個參數:when(path, route)
            ex:
                $routeProvider.when(url,{
                    template:string,
                    templateUrl: string,
                    controller: string, function or array,
                    controllerAs: string,
                    redirectTo: string or function,
                    reloadOnSearch: true or false,
                    resolve: object<key, function>
                });

                path:路由路徑,與$location.path(即為當前的URL路徑)進行匹配。可用$routeParams讀取這個參數
                route:配置對象。當路徑匹配成功時,具體該做些什麼。其中屬性包含了controllertemplatetemplateURLresolveredirectToreloadOnSearch

                    controller
                        controller: 'MyController'
                        controller: function($scope) {}
                    template
                        template: '<div><h2>Route</h2></div>'
                    templateURL
                        templateUrl: 'views/template_name.html'
                    redirectTo
                        redirectTo: '/home'
                        redirectTo: function(route,path,search)
                    reloadOnSearch
                        reloadOnSearch:true(default)
                        reloadOnSearch:false
                        $location.search()$location.hash()發生變化時會重新加載路由。
                    resolve
                        載入特定route前,預先執行一個非同步工作
            */
            .when('/', {
                templateUrl: 'views/home.html',
                controller: 'HomeController'
            })
            .when('/temp', {
                template: '<h5>This is template route.</h5>',
                controller: 'TempController'
            })
            .when('/login', {
                templateUrl: 'views/login.html',
                controller: 'LoginController'
            })
            .when('/dashboard', {
                templateUrl: 'views/dashboard.html',
                controller: 'DashboardController',
                resolve: {
                    user: function (SessionService) {
                        return SessionService.getCurrentUser();
                    }
                }
            })
//使用otherwise來定義預設路由
            .otherwise({
                redirectTo: '/'
            });
        }]);
    </script>
</body>
</html>

1 則留言:

  1. emperor casino | Shootercasino
    emperor casino | Shootingercasino | Shootingercasino.com | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 제왕 카지노 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 샌즈카지노 | | 메리트 카지노 주소 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

    回覆刪除