gpt4 book ai didi

angularJS Controller 第一次不加载

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:37 25 4
gpt4 key购买 nike

在 View 中,我声明:

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

请注意,包含 Controller 的 Angular 文件在上面的代码行之前被正确调用。

在我的 Controller 内,我尝试使用 $http get 请求从 Node api 加载数据,并将其结果设置为如下范围:

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

更新二

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

最佳答案

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

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

关于angularJS Controller 第一次不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28279357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com