2016-03-26 30 views
0

我有一个登录控制器控制范围变量不加载,直到刷新

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页面打开后立即填充列表?

+0

不确定,但更改 $ scope.roomNames = response; 。 到 $范围$申请(函数(){$ = scope.roomNames响应; }) –

+0

尝试返回$ http.get – user2950720

+0

当页面加载第一次没有叫这个功能呢? –

回答

0

尝试使用从$ HTTP成功和errorcallbacks,倒不如将此功能放到一个工厂

myApp.controller('HomeCtrl', ['$scope', '$http', '$location', 
 
    function($scope, $http, $location) { 
 
    console.log("Hello World from home controller"); 
 

 
    var refresh = function() { 
 
     $http.get('/roomNames') 
 
     .then(function successCallback(response) { 
 
      $scope.roomNames = response; 
 
     }, function errorCallback(response) { 
 
      //output an error if failed? 
 
     }); 
 
    } 
 
    refresh(); 
 
    } 
 
]);

+0

这仍然行不通..我认为使用服务可能是我最好的选择! – pd176

0

这是因为,在你面前你移动到主页你访问的服务器。因此,在主页上,您将无法获取数据,并在刷新后获取它们。

只有在成功验证后才重定向到页面。

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'); // Redirect after success login 
     }) 
     .catch(function(data){ 
      console.error("Error in login",data); 
     }); 

    }, function myError(err) { 
     console.log(err); 
     alert("Invalid username/password") 
    }); 
}; 
}]); 
+0

不工作..路径根本不会改变 – pd176

+0

什么不起作用?在'roomNames'变量中显示数据。 –

+0

当我点击登录按钮时,路径应该改变为home.html,但这并没有发生。我卡在login.html页面 – pd176