【问题标题】:Controller scope variable not loading until refresh控制器范围变量在刷新之前不加载
【发布时间】:2016-07-14 00:23:49
【问题描述】:

我有一个登录控制器

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 中。这是我的 home.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.$apply(function(){ $scope.roomNames = response; })
  • 尝试返回$http.get
  • 第一次加载页面时会调用这个函数吗?
  • 这是因为,您在访问服务器之前转到home 页面。所以在首页,你不会获取到数据,刷新后获取。

标签: javascript html angularjs


【解决方案1】:

尝试使用 $http 中的成功和错误回调,最好将此功能放入工厂中

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();
  }
]);

【讨论】:

  • 这仍然不起作用..我认为使用服务可能是我最好的选择!
【解决方案2】:

这是因为,您在访问服务器之前先转到主页。因此,在首页,你不会获取数据,刷新后获取。

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

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")
    });
};
}]);

【讨论】:

  • 究竟是什么不起作用?在roomNames 变量中显示数据。
  • 当我点击登录按钮时,路径应该会更改为 home.html,但这不会发生。我卡在 login.html 页面
  • 意思是,你的电话$http.post('/login',details.data) 没有成功。添加catch方法。
猜你喜欢
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多