AngularJS Multiple ng-app within a page

后端 未结 13 2194
闹比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:11

    You can use angular.bootstrap() directly... the problem is you lose the benefits of directives.

    First you need to get a reference to the HTML element in order to bootstrap it, which means your code is now coupled to your HTML.

    Secondly the association between the two is not as apparent. With ngApp you can clearly see what HTML is associated with what module and you know where to look for that information. But angular.bootstrap() could be invoked from anywhere in your code.

    If you are going to do it at all the best way would be by using a directive. Which is what I did. It's called ngModule. Here is what your code would look like using it:

    <!DOCTYPE html>
    <html>
        <head>
            <script src="angular.js"></script>
            <script src="angular.ng-modules.js"></script>
            <script>
              var moduleA = angular.module("MyModuleA", []);
              moduleA.controller("MyControllerA", function($scope) {
                  $scope.name = "Bob A";
              });
    
              var moduleB = angular.module("MyModuleB", []);
              moduleB.controller("MyControllerB", function($scope) {
                  $scope.name = "Steve B";
              });
            </script>
        </head>
        <body>
            <div ng-modules="MyModuleA, MyModuleB">
                <h1>Module A, B</h1>
                <div ng-controller="MyControllerA">
                    {{name}}
                </div>
                <div ng-controller="MyControllerB">
                    {{name}}
                </div>
            </div>
    
            <div ng-module="MyModuleB">
                <h1>Just Module B</h1>
                <div ng-controller="MyControllerB">
                    {{name}}
                </div>
            </div>
        </body>
    </html>
    

    You can get the source code for it at:

    http://www.simplygoodcode.com/2014/04/angularjs-getting-around-ngapp-limitations-with-ngmodule/

    It's implemented in the same way as ngApp. It simply calls angular.bootstrap() behind the scenes.

    0 讨论(0)
  • 2020-11-22 02:14

             var shoppingCartModule = angular.module("shoppingCart", [])
              shoppingCartModule.controller("ShoppingCartController",
               function($scope) {
                 $scope.items = [{
                   product_name: "Product 1",
                   price: 50
                 }, {
                   product_name: "Product 2",
                   price: 20
                 }, {
                   product_name: "Product 3",
                   price: 180
                 }];
                 $scope.remove = function(index) {
                   $scope.items.splice(index, 1);
                 }
               }
             );
             var namesModule = angular.module("namesList", [])
              namesModule.controller("NamesController",
               function($scope) {
                 $scope.names = [{
                   username: "Nitin"
                 }, {
                   username: "Mukesh"
                 }];
               }
             );
    
    
             var namesModule = angular.module("namesList2", [])
              namesModule.controller("NamesController",
               function($scope) {
                 $scope.names = [{
                   username: "Nitin"
                 }, {
                   username: "Mukesh"
                 }];
               }
             );
    
    
             angular.element(document).ready(function() {
               angular.bootstrap(document.getElementById("App2"), ['namesList']);
               angular.bootstrap(document.getElementById("App3"), ['namesList2']);
             });
    <!DOCTYPE html>
    <html>
    
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
    </head>
    
    <body>
    
      <div id="App1" ng-app="shoppingCart" ng-controller="ShoppingCartController">
        <h1>Your order</h1>
        <div ng-repeat="item in items">
          <span>{{item.product_name}}</span>
          <span>{{item.price | currency}}</span>
          <button ng-click="remove($index);">Remove</button>
        </div>
      </div>
    
      <div id="App2" ng-app="namesList" ng-controller="NamesController">
        <h1>List of Names</h1>
        <div ng-repeat="_name in names">
          <p>{{_name.username}}</p>
        </div>
      </div>
      <div id="App3" ng-app="namesList2" ng-controller="NamesController">
        <h1>List of Names</h1>
        <div ng-repeat="_name in names">
          <p>{{_name.username}}</p>
        </div>
      </div>
    
    
    </body>
    
    </html>

    0 讨论(0)
  • 2020-11-22 02:14

    // root-app
    const rootApp = angular.module('root-app', ['app1', 'app2E']);
    
    // app1
    const app11aa = angular.module('app1', []);
    app11aa.controller('main', function($scope) {
      $scope.msg = 'App 1';
    });
    
    // app2
    const app2 = angular.module('app2E', []);
    app2.controller('mainB', function($scope) {
      $scope.msg = 'App 2';
    });
    
    // bootstrap
    angular.bootstrap(document.querySelector('#app1a'), ['app1']);
    angular.bootstrap(document.querySelector('#app2b'), ['app2E']);
    <!-- angularjs@1.7.0 -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.0/angular.min.js"></script>
    
    <!-- root-app -->
    <div ng-app="root-app">
    
      <!-- app1 -->
      <div id="app1a">
        <div ng-controller="main">
          {{msg}}
        </div>
      </div>
    
      <!-- app2 -->
      <div id="app2b">
        <div ng-controller="mainB">
          {{msg}}
        </div>
      </div>
    
    </div>

    0 讨论(0)
  • 2020-11-22 02:16
    <html>
    <head>
        <script src="angular.min.js"></script>
    </head>
    <body>
    <div ng-app="shoppingCartParentModule" >
         <div ng-controller="ShoppingCartController">
            <h1>Your order</h1>
            <div ng-repeat="item in items">
                <span>{{item.product_name}}</span>
                <span>{{item.price | currency}}</span>
                <button ng-click="remove($index);">Remove</button>
            </div>
        </div>
    
        <div ng-controller="NamesController">
            <h1>List of Names</h1>
            <div ng-repeat="name in names">
                <p>{{name.username}}</p>
            </div>
        </div>
    </div>
    </body>
    <script>
    var shoppingCartModule = angular.module("shoppingCart", [])
                shoppingCartModule.controller("ShoppingCartController",
                    function($scope) {
                        $scope.items = [
                            {product_name: "Product 1", price: 50},
                            {product_name: "Product 2", price: 20},
                            {product_name: "Product 3", price: 180}
                        ];
                        $scope.remove = function(index) {
                            $scope.items.splice(index, 1);
                        }
                    }
                );
                var namesModule = angular.module("namesList", [])
                namesModule.controller("NamesController",
                    function($scope) {
                        $scope.names = [
                            {username: "Nitin"},
                            {username: "Mukesh"}
                        ];
                    }
                );
       angular.module("shoppingCartParentModule",["shoppingCart","namesList"])
    </script>
    </html>
    
    0 讨论(0)
  • 2020-11-22 02:17

    Only one app is automatically initialized. Others have to manually initialized as follows:

    Syntax:

    angular.bootstrap(element, [modules]);
    

    Example:

    <!DOCTYPE html>
    <html>
    
    <head>
      <script src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8" data-require="angular.js@1.5.8"></script>
      <script data-require="ui-router@0.2.18" data-semver="0.2.18" src="//cdn.rawgit.com/angular-ui/ui-router/0.2.18/release/angular-ui-router.js"></script>
      <link rel="stylesheet" href="style.css" />
      <script>
        var parentApp = angular.module('parentApp', [])
      .controller('MainParentCtrl', function($scope) {
        $scope.name = 'universe';
      });
    
    
    
    var childApp = angular.module('childApp', ['parentApp'])
      .controller('MainChildCtrl', function($scope) {
        $scope.name = 'world';
      });
    
    
    angular.element(document).ready(function() {
      angular.bootstrap(document.getElementById('childApp'), ['childApp']);
    });
        
      </script>
    </head>
    
    <body>
      <div id="childApp">
        <div ng-controller="MainParentCtrl">
          Hello {{name}} !
          <div>
            <div ng-controller="MainChildCtrl">
              Hello {{name}} !
            </div>
          </div>
        </div>
      </div>
    </body>
    
    </html>

    AngularJS API

    0 讨论(0)
  • 2020-11-22 02:19

    You can merge multiple modules in one rootModule , and assign that module as ng-app to a superior element ex: body tag.

    code ex:

        <!DOCTYPE html>
    <html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="namesController.js"></script>
    <script src="myController.js"></script>
    <script>var rootApp = angular.module('rootApp', ['myApp1','myApp2'])</script>
    <body ng-app="rootApp">
    
    <div ng-app="myApp1" ng-controller="myCtrl" >
    First Name: <input type="text" ng-model="firstName"><br>
    Last Name: <input type="text" ng-model="lastName"><br>
    <br>
    Full Name: {{firstName + " " + lastName}}
    </div>
    
    <div ng-app="myApp2" ng-controller="namesCtrl">
    <ul>
      <li ng-bind="first">{{first}}
      </li>
    </ul>
    </div>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题