AngularJS의 ngroute 라우팅 참조

2634 단어 Angularjs
출처


    
    
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"/>
    
    <script src="http://cdn.bootcss.com/angular.js/1.2.9/angular-route.min.js"/>




    <div ng-app="myApp">
        <div class="header">
            <a href="#div1">   div</a>
            <a href="#div2">   div</a>
            <a href="#div3">   div</a>
            <a href="#content/13">     </a>
        </div>

    <div ng-view=""/>
    </div>

</code></pre> 
  <br/> 
  <pre><code>var app = angular.module("myApp", ['ngRoute']);

    app.config(['$routeProvider', function ($routeProvider) {
        $routeProvider
            .when('/div1', {
                template: '<p>  div1{{text}}</p>',
                controller: 'div1Controller'
            })
            .when('/div2', {
                template: '<p>  div2{{text}}</p>',
                    controller: 'div2Controller'
            })
             .when('/div3', {
                 template: '<p>  div3{{text}}</p>',
                 controller: 'div3Controller'
             })
          .when('/content/:id', {
              template: '<p>  content{{id}}</p>',
              controller: 'div4Controller'
          })
            .otherwise({
                redirectTo: '/div1'

            });
    }]);

    app.controller('div1Controller', function ($scope) {
        $scope.text = 'phonegap   div1Controller';
    })

    app.controller('div2Controller', function ($scope) {
        $scope.text = 'phonegap   div2Controller';
    })
    
    app.controller('div3Controller', function ($scope) {
        $scope.text = 'phonegap   div3Controller';
    })

    app.controller('div4Controller', function ($scope, $routeParams, $location) {
        
        if ($routeParams.id==0) {
            $location.path('/div1');//      
        }
        console.log($routeParams);
        $scope.id = $routeParams.id;
        $scope.text = 'phonegap   div3Controller';

    })</code></pre> 
  <br/> 
  <br/> 
   
 </div> 
</div>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기