AngularJS Multiple ng-app within a page

后端 未结 13 2201
闹比i
闹比i 2020-11-22 01:48

I have just started learning Angular JS and created some basic samples however I am stuck with the following problem.

I have created 2 modules and 2 controllers.

13条回答
  •  情深已故
    2020-11-22 02:20

    Use angular.bootstrap(element, [modules], [config]) to manually start up AngularJS application (for more information, see the Bootstrap guide).

    See the following example:

    // root-app
    const rootApp = angular.module('root-app', ['app1', 'app2']);
    
    // app1
    const app1 = angular.module('app1', []);
    app1.controller('main', function($scope) {
      $scope.msg = 'App 1';
    });
    
    // app2
    const app2 = angular.module('app2', []);
    app2.controller('main', function($scope) {
      $scope.msg = 'App 2';
    });
    
    // bootstrap
    angular.bootstrap(document.querySelector('#app1'), ['app1']);
    angular.bootstrap(document.querySelector('#app2'), ['app2']);
    
    
    
    
    
    {{msg}}
    {{msg}}

提交回复
热议问题