【问题标题】:Angular load data from server and display information on separate page从服务器角度加载数据并在单独的页面上显示信息
【发布时间】:2017-03-12 13:52:12
【问题描述】:

我有一个显示所有用户名的页面。现在我想单击其中一个用户名调用服务器以检索更多信息并将其显示在单独的用户页面上。 (名字、姓氏等)

我的问题是,当我单击用户名页面打开时,但未填充字段。您能否检查我的代码并建议我在那里做错了什么?

app.config(function($routeProvider) {
    $routeProvider
        .when("/", {
            templateUrl : "pages/login_page.html"
        })
        .when("/userpage", {
            controller : 'UserController',
            templateUrl : "pages/user_page.html"
        })
        .when("/allusers", {
            controller : 'AllUserController',
            templateUrl : "pages/all_users.html"
        });
});

这是我的登录代码。用户认证后,它可以看到所有其他用户。所以我将视图更改为#allusers

app.directive("loginForm", function (AuthorizedHttp, ConfigurationRepository, UserObj) {
        return {
            restrict: 'E',
            scope: {
            },
            templateUrl: 'templates/login-template.html',
            replace: 'true',
            controller: ['$scope', '$http', '$window',
                function($scope, $location, $window) {
                    $scope.loginError = false;
                    $scope.login = function () {
                        $scope.loginError = false;
                        UserObj.setState(null, null, $scope.username, $scope.password, null);
                        AuthorizedHttp.get('http://{0}/account/user/login'.format(ConfigurationRepository.getBackendHostName()))
                            .success(function (response) {
                                UserObj.setState(response.first_name, response.last_name, response.email, $scope.password, response.role, response.timezones);
                                $window.location = "#allusers";
                            })
                            .error(function (err, status) {
                                $scope.username = '';
                                $scope.password = '';
                                $scope.loginError = true;
                            })
                    }
                }
            ]
        }
    });

下面的代码负责拨打电话并检索所有users。工作正常。

app.controller('AllUserController', function ($scope, AuthorizedHttp, ConfigurationRepository, UserObj, UserCurrent, TimezoneService) {

    $scope.init = function () {
        TimezoneService.getAllUsers()
            .success(function (response) {
                $scope.users_emails = response.map(function (item) {return item.email})
            })
            .error(function (err, status) {
                alert('Error loading all users ')
            });
    };

});

HTML 显示所有用户名。还可以设置 ng-click 以传递用户名作为参数来检索所需的用户。

<div ng-controller="AllUserController"  ng-init="init()">
    <div ng-controller="UserController">
        <div ng-repeat="email in users_emails" class="item-unchecked">
            <a ng-click="getUser(email)">{{email}}</a>
        </div>
    </div>
</div>

用户控制器。每次单击用户名链接时执行。

app.controller('UserController', function ($scope, AuthorizedHttp, ConfigurationRepository, UserObj, UserCurrent, TimezoneService,  $window) {
$scope.user_display_name = 'Now set yet';

$scope.getUser = function(username) {
    TimezoneService.getUser(username)
        .then(function (response) {
            $scope.required_user = response.data;
            $scope.user_display_name = '{0} ({1})'.format(response.data.first_name, response.data.email);
            $scope.user_timezones = response.data.timezones.map(function (item) {
                return item.timezone_name
            });
            $scope.user_role = response.data.role;
            $window.location = '#userpage';
        });
};

});

结果 user_page.html 已加载,但未设置所有字段。我不明白为什么,因为我在更改$window.location 之前设置了范围值。

【问题讨论】:

  • #userpage 是否也使用 UserController
  • 你能提供一个plnkr吗?

标签: javascript angularjs routing


【解决方案1】:
  1. 从您的 HTML 中删除 ng-controller="UserController"
  2. 在你的 AllUserController 中创建一个函数,就像 $scope.customNavigate = function(routeToNavigate, routeParameter){ $location.path("/" + routeToNavigate + "/" + routeParameter); }
  3. .when("/userpage", { 更改为.when("/userpage/:email", {
  4. ng-click="getUser(email)" 更改为ng-click="customNavigate("userpage", email)"

  5. $routeParams 注入您的用户控制器

  6. $scope.getUser = function(username) { 更改为function getUser (username) {
  7. 在您的 UserController 中调用 getUser($routeParams.email)

【讨论】:

  • 成功了,谢谢!但是,它会在数据到达之前加载页面并延迟填充所有字段。在收到响应或解决响应之前阻止页面加载的好模式是什么?
  • 为此有一个名为“resolve”的关键字。您应该在您想要延迟到数据到达的路线中使用它。这些链接将对您有所帮助。 Link1 Link2 顺便说一句,我强烈建议你使用 ui-router 而不是 ngRouter。它更灵活。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 2017-12-09
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多