angularJS控制器第一次没有加载

时间:2015-02-02 13:57:54

标签: angularjs node.js angularjs-scope

在视图中,我宣布:

<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>

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

在我的控制器中,我正在尝试从节点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');
      });

只有在多次刷新页面后才能使用。有什么我做错了吗?

更新

以下是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'); 
});

更新II

我在Firefox中尝试使用相同的代码而不是chrome,它似乎正常运行有关如何修复此问题的任何想法。

1 个答案:

答案 0 :(得分:1)

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

类似问题:Angularjs [$rootScope:inprog] inprogress error

相关问题