bootstrap使用多个ng-app

时间:2014-02-20 09:01:53

标签: angularjs

我按照AngularJS Multiple ng-app within a page

中的代码进行操作

我不知道我错过了什么。

    var reviewApp = angular.module("reviewApp", []);
var reviewApp2 = angular.module("reviewApp2", []);

reviewApp.controller("reviewCtrl",
                     function ShippingAddressCtrl($scope, $http) {
    $scope.name='leo1';
});

reviewApp2.controller("productController",
                     function ProductController($scope, $http) {
    $scope.name1='leo2';
});
angular.bootstrap(document.getElementById("div2"),['reviewApp2']);

http://jsfiddle.net/lordango/X3ZsU/1/

1 个答案:

答案 0 :(得分:9)

当你引导多个应用程序时,你不能多次使用ng-app属性,angular会在标记中取第一个并自动引导它,所有其他ng-app被忽略而你有手动引导它们

var reviewApp = angular.module("reviewApp", []);
var reviewApp2 = angular.module("reviewApp2", []);

reviewApp.controller("reviewCtrl",
                     function ShippingAddressCtrl($scope, $http) {
    $scope.name='leo1';
});

reviewApp2.controller("productController",
                     function ProductController($scope, $http) {
    $scope.name1='leo2';
});
angular.element(document).ready(function() {
    angular.bootstrap(document.getElementById("div1"),['reviewApp']);
    angular.bootstrap(document.getElementById("div2"),['reviewApp2']);
});

HTML

<div id="div1">
<div ng-controller="reviewCtrl">{{name}}</div>
</div>

<div id="div2">
<div ng-controller="productController">{{name1}}</div>
</div>