代码之家  ›  专栏  ›  技术社区  ›  David Klempfner

div中的第二个angularjs应用程序表达式不工作[重复]

  •  0
  • David Klempfner  · 技术社区  · 5 年前

    我正在学习angularjs,并试图找出为什么在第二个表达式 <div> 不起作用。它显示:

    {{firstname1+“”+lastname1}

    而不是:

    约翰1 DOE1

    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <body>
    
    <div ng-app="myApp" ng-controller="myCtrl">
    {{ firstName + " " + lastName }}
    </div>
    
    <div ng-app="myApp1" ng-controller="myCtrl1">
    {{ firstName1 + " " + lastName1 }}
    </div>
    
    <script>
    var app = angular.module("myApp", []);
    app.controller("myCtrl", function($scope) {
        $scope.firstName = "John";
        $scope.lastName = "Doe";
    });
    
    var app1 = angular.module("myApp1", []);
    app1.controller("myCtrl1", function($scope) {
        $scope.firstName1 = "John1";
        $scope.lastName1 = "Doe1";
    });
    
    </script>
    
    </body>
    </html>
    

    编辑:

    为了让它工作,我需要移除 ng-app="myApp1" ? 我要把什么论点传给 angular.bootstrap() 让它工作?

    1 回复  |  直到 5 年前
        1
  •  1
  •   georgeawg    5 年前

    使用ngapp时需要记住以下几点:

    • 每个html文档只能自动引导一个angularjs应用程序。文档中找到的第一个ngapp将用于定义根元素以作为应用程序自动引导。要在HTML文档中运行多个应用程序,必须使用 angular.bootstrap 相反。

    AngularJS ng-app Directive API Reference