我可以在角度模塊中加載一次數據嗎?我嘗試使用.run(),但每次訪問頁面時都會調用它。舉例來說,假設有屬於同一模塊2個的HTML頁面:有沒有像角度初始化模塊一次?
TestPage1.html:
<html ng-app="myApp">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="js/angular.min.js"></script>
<script src="js/jquery-1.8.2.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<p><a ng-href="TestPage2.html">Go to Page2</a></p>
</body>
</html>
TestPage2.html:
<html ng-app="myApp">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="js/angular.min.js"></script>
<script src="js/jquery-1.8.2.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<p><a ng-href="TestPage1.html">Go to Page1</a></p>
</body>
</html>
app.js:
var myApp = angular.module('myApp', []);
var cnt = 0;
myApp.run(['$rootScope', '$http', function(scope, $http) {
if(scope.loggedIn == undefined || scope.loggedIn == null) {
$http.get('rest/userData').success(function(data) {
cnt++;
alert(cnt);
scope.loggedIn = true;
});
}
}]);
,當我瀏覽從一個網頁到另一個此.RUN()是越來越一再呼籲與CNT爲1。是否有可能有它會在模塊的生命週期中調用一次初始化?或者其他方式是什麼?
請指定您想要實現的目的 – 2013-04-25 11:57:40
您在應用程序中缺少控制器和其他一些項目 – lucuma 2013-04-25 13:20:15