Multiple controllers with AngularJS in single page app

前端 未结 7 737
不思量自难忘°
不思量自难忘° 2020-12-22 16:44

I want to know is how to use multiple controllers for a single page application. I have tried to figure it out and I\'ve found questions very similar to mine, but there is j

7条回答
  •  甜味超标
    2020-12-22 17:05

    I'm currently in the process of building a single page application. Here is what I have thus far that I believe would be answering your question. I have a base template (base.html) that has a div with the ng-view directive in it. This directive tells angular where to put the new content in. Note that I'm new to angularjs myself so I by no means am saying this is the best way to do it.

    app = angular.module('myApp', []);                                                                             
    
    app.config(function($routeProvider, $locationProvider) {                        
      $routeProvider                                                                
           .when('/home/', {                                            
             templateUrl: "templates/home.html",                                               
             controller:'homeController',                                
            })                                                                      
            .when('/about/', {                                       
                templateUrl: "templates/about.html",     
                controller: 'aboutController',  
            }) 
            .otherwise({                      
                template: 'does not exists'   
            });      
    });
    
    app.controller('homeController', [              
        '$scope',                              
        function homeController($scope,) {        
            $scope.message = 'HOME PAGE';                  
        }                                                
    ]);                                                  
    
    app.controller('aboutController', [                  
        '$scope',                               
        function aboutController($scope) {        
            $scope.about = 'WE LOVE CODE';                       
        }                                                
    ]); 
    

    base.html

    
    
    
        

提交回复
热议问题