我有一个登录控制器控制范围变量不加载,直到刷新
myApp.controller('LoginCtrl', ['$scope', '$http','$location', function($scope, $http, $location) {
$scope.login = function() {
var data = JSON.stringify({user: $scope.user.id, password: $scope.user.password, type: "m.login.password"});
$http.post("http://localhost:8008/_matrix/client/api/v1/login", data).then(function mySuccess(details){
$http.post('/login',details.data).success(function(response){
console.log(response);
});
$location.path('home');
}, function myError(err) {
console.log(err);
alert("Invalid username/password")
});
};
}]);
和家庭控制器
myApp.controller('HomeCtrl', ['$scope', '$http','$location', function($scope, $http, $location) {
console.log("Hello World from home controller");
var refresh = function() {
$http.get('/roomNames').success(function(response) {
console.log("I got the data I requested");
$scope.roomNames = response;
});
}
refresh();
}]);
正如所看到的,如果登录信息是正确的,LoginCtrl改变路线是home.html的。
但是,当我运行应用程序并成功登录时,HomeCtrl应该向服务器发出获取请求以获取登录用户的数据。
我想要的是将这些数据作为home.html中的列表加载。这是我的家.html
<h3 class="subHeader"> Rooms </h3>
<ul class="nav nav-sidebar">
<!-- <li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li> -->
<li ng-repeat="room in roomNames"><a>{{room}}</a></li>
</ul>
但是,成功登录时,roomNames变量最初为空。只要刷新页面,html列表就会被填充。
如何在home.html页面打开后立即填充列表?
不确定,但更改 $ scope.roomNames = response; 。 到 $范围$申请(函数(){$ = scope.roomNames响应; }) –
尝试返回$ http.get – user2950720
当页面加载第一次没有叫这个功能呢? –