在Angular中,一个页面只有第一个ng-app起作用,若想启动多个ng-app的话,可以手动加载,只需要再加一行代码即可。
如下:
<body>
<!-- 可以手动装载除了第一个以外的多个ng-app -->
<div id="app1div" ng-app="myApp1" ng-controller="personCtrl">
<p>personCtrl</p>
First name:<input type="text" ng-model="firstName"/>
Last name:<input type="text" ng-model="lastName"/>
Full name:{{ fullName() }}
</div><br><br>
<div id="app2div" ng-app="myApp2" ng-controller="personCtrl">
<p>personCtrl</p>
First name:<input type="text" ng-model="firstName"/>
Last name:<input type="text" ng-model="lastName"/>
Full name:{{ fullName() }}
</div>
<script type="text/javascript">
var app1 = angular.module('myApp1',[]);
var app2 = angular.module('myApp2',[]);
app1.controller('personCtrl',function($scope){
$scope.firstName = 'Aaron';
$scope.lastName = 'Smith';
$scope.fullName = function(){ return $scope.firstName + $scope.lastName; }
});
app2.controller('personCtrl',function($scope){
$scope.firstName = 'Andy';
$scope.lastName = 'Wi';
$scope.fullName = function(){ return $scope.firstName + $scope.lastName; }
});
angular.bootstrap(document.getElementById("app2div"),['myApp2']);
</script>
</body>