angularJS控制器第一次不加载

在视图里面,我宣称:

<div ng-controller="LoginController"> <div ng-if="authenticated=='no'"> <a href="/signin">Sign In</a> </div> <div ng-if="authenticated=='yes'"> <a href="/logout">Logout</a> </div> </div> 

请注意,在上面的代码行之前正确调用包含控制器的angular度文件。

在我的控制器里面,我试图从一个节点API加载数据,用$http get请求,并将其结果设置为如下所示的范围:

 var indexModule = angular.module ('indexModule', []); indexModule.controller('LoginController',['$scope', '$http', function($scope, $http){ $http.get('/api/data') .success(function(data){ $scope.authenticated = data.authenticated; }) .error(function(){ alert('Error occured'); }); 

这只在几个页面刷新后才起作用。 有什么我做错了吗?

UPDATE

这里是node.js路由:

 app.get('/api/data', function (req,res) { // this is called by the controller to populate $scope var sendtoFront = new Object(); toFront.authenticated = req.isAuthenticated() ? 'yes' : 'no'; if(req.isAuthenticated()) { toFront.session = req.session.passport; } res.send(toFront); }); app.get('/', function (req,res) { // this load the view res.render('index'); }); 

更新二

我尝试了相同的代码在Firefox而不是铬,它似乎正常工作,有关如何修复这个问题的任何想法。

编辑:在控制台中读取错误后,它似乎不需要$ scope。$ apply()。 看来你已经从$ rootscope手动调用了$ apply()。 如果您手动在$ rootScope上调用$ apply,请检入其他代码部分。

类似的问题: Angularjs [$ rootScope:inprog]进度错误