AngularJS方法 —— angular.bootstrap


描述:
  此方法用於手動加載angularjs模板
  (官方翻譯:注意基於端到端的測試不能使用此功能來引導手動加載,他們必須使用ngapp。 angularjs會檢測這個模板是否被瀏覽器加載或者加載多次並且在控制台給出警告在加載其他模塊的時候,這防止了奇怪的結果,在實際應用中,angularjs在嘗試其它的多個  實例來研究DOM)。

 

使用方法:

  angular.bootstrap(element, [modules], [config]);

 

參數:

參數名稱 參數類型 描述
element DOMElement DOM元素
modules Array 要加載的模板
config Object 配置選項的對象。

 

返回值:

  返回這個應用程序的新創建的injector 對象。

 

實例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src="js/angular.min.js"></script>       
    </head>
    <body>
        <div ng-controller="WelcomeController">
           <span ng-bind="greeting"></span>
        </div>
        <script type="text/javascript">
            var app = angular.module('demo', []).controller('WelcomeController', function ($scope) {
                $scope.greeting = 'Welcome!';
            });
            angular.bootstrap(document, ['demo']);
        </script>
    </body>
</html>

等同於:

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src="js/angular.min.js"></script>       
    </head>
    <body ngApp="myApp">
        <div ng-controller="WelcomeController">
           <span ng-bind="greeting"></span>
        </div>
        <script type="text/javascript">
            var app = angular.module('myApp', []).controller('WelcomeController', function ($scope) {
                $scope.greeting = 'Welcome!';
            });
            //angular.bootstrap(document, ['demo']);
        </script>
    </body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM