我在一個頁面中使用兩個模塊,第二個模塊不工作。如果我刪除第一個模塊,然後第二個工作正常。我不知道爲什麼會發生這種情況,因爲我們可以在一個頁面中使用兩個或多個模塊。爲什麼第二個模塊不能在angularjs中工作?
<html>
<head>
<title>Angular JS</title>
<script src="angular.min.js"></script>
</head>
<body>
<p>------------ New Controller ------------</p>
<div ng-app="newApp" ng-controller="validateform" >
{{name}}
<form name="newForm">
Enter Name: <input type="text" id="firstName" name="firstName"
ng-model="firstName" ng-maxlength="20"> </br>
<button ng-click="show()" >Submit</button>
</form>
</div>
<script>
//Creating AngularJS controller here
var newApp = angular.module('newApp',[]);
newApp.controller('validateform',function($scope){
$scope.name="newApp";
$scope.show = function() {
if($scope.firstName == undefined || $scope.firstName == '')
alert('Name is Required');
else
alert('Hi - ' + $scope.firstName);
};
}
);
</script>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp" ng-controller="myController">
{{name}}
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('myController',function($scope){
$scope.name="rishi";
});
</script>
</body>
</html>
您應該每個頁面只有一個模塊充當應用程序。其他模塊應該是依賴關係。 – MikeSW
[如何在一個頁面中定義兩個角度應用程序/模塊?](http://stackoverflow.com/questions/12860595/how-to-define-two-angular-apps-modules-in-one-page ) – MikeSW