发布于 4年前

Angularjs ui-router的简单使用


<html>

<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    <script src="https://cdn.bootcss.com/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>
</head>

<body ng-app='myApp' ng-controller='myCtrl'>
    <div>
        <button ng-click="clickBtn('home')"><a href="javascript:">首页</a></button>
        <button ng-click="clickBtn('two')"><a href="javascript:">two</a></button>
    </div>
    <div id='main'>
        <div ui-view></div>
    </div>
    <script type="text/javascript">
    angular.module('myApp', ['ui.router'])
        .controller('myCtrl', ['$scope', '$state', function($scope, $state) {
            $scope.test = 'this is a test data';
            $scope.clickBtn = function(url) {
                var searchs = {
                    a: 'test data'
                };
                $state.go(url, { "p": JSON.stringify(searchs) });
            };
        }])
        .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
            $stateProvider
                // .state('two',{template : '<div ui-view></div>',url : '/two'})
                .state('home', {
                    url: '/home?:p',
                    template: '<div>home</div>',
                    controller: 'rcHome'
                })
                .state('two', {
                    url: '/two?:p',
                    template: '<div>two</div>',
                    controller: 'rcTwo'
                });

            $urlRouterProvider.otherwise('/home');
        }])
        .controller('rcHome', ['$scope', function($scope) {
            //
        }])
        .controller('rcTwo', ['$scope', function($scope) {
            //
        }])
    </script>
</body>

</html>
©2020 edoou.com   京ICP备16001874号-3