AngularJS Multiple ng-app within a page

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

    You can define a Root ng-App and in this ng-App you can define multiple nd-Controler. Like this

        <!DOCTYPE html>
        <html>
        <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    
    <style>
             table, th , td {
                border: 1px solid grey;
                border-collapse: collapse;
                padding: 5px;
             }
    
             table tr:nth-child(odd) {
                background-color: #f2f2f2;
             }
    
             table tr:nth-child(even) {
                background-color: #ffffff;
             }
          </style>
    
         <script>
          var mainApp = angular.module("mainApp", []);
    
          mainApp.controller('studentController1', function ($scope) {
          $scope.student = {
          firstName: "MUKESH",
          lastName: "Paswan",
    
          fullName: function () {
             var studentObject;
             studentObject = $scope.student;
             return studentObject.firstName + " " + studentObject.lastName;
                         }
                     };
                 });
    
                 mainApp.controller('studentController2', function ($scope) {
                     $scope.student = {
                         firstName: "Mahesh",
                         lastName: "Parashar",
                         fees: 500,
    
                         subjects: [
                            { name: 'Physics', marks: 70 },
                            { name: 'Chemistry', marks: 80 },
                            { name: 'Math', marks: 65 },
                            { name: 'English', marks: 75 },
                            { name: 'Hindi', marks: 67 }
                         ],
    
                         fullName: function () {
                             var studentObject;
                             studentObject = $scope.student;
                             return studentObject.firstName + " " + studentObject.lastName;
                         }
                     };
                 });
              </script>
    
        <body>
        <div ng-app = "mainApp">
        <div id="dv1"  ng-controller = "studentController1">
        Enter first name: <input type = "text" ng-model = "student.firstName"><br/><br/> Enter last name: <input type = "text" ng-model = "student.lastName"><br/>
        <br/>
         You are entering: {{student.fullName()}}
        </div>
    
        <div id="dv2" ng-controller = "studentController2">
         <table border = "0">
                    <tr>
                       <td>Enter first name:</td>
                       <td><input type = "text" ng-model = "student.firstName"></td>
                    </tr>
    
                    <tr>
                       <td>Enter last name: </td>
                       <td>
                          <input type = "text" ng-model = "student.lastName">
                       </td>
                    </tr>
    
                    <tr>
                       <td>Name: </td>
                       <td>{{student.fullName()}}</td>
                    </tr>
    
                    <tr>
                       <td>Subject:</td>
    
                       <td>
                          <table>
                             <tr>
                                <th>Name</th>.
                                <th>Marks</th>
                             </tr>
    
                             <tr ng-repeat = "subject in student.subjects">
                                <td>{{ subject.name }}</td>
                                <td>{{ subject.marks }}</td>
                             </tr>
    
                          </table>
                       </td>
    
                    </tr>
                 </table>
    
              </div>
        </div>
    
        </body>
        </html>
    
    0 讨论(0)
提交回复
热议问题