【问题标题】:Selecting first tab by default when page is loaded in angularJS在angularJS中加载页面时默认选择第一个选项卡
【发布时间】:2019-02-11 19:58:19
【问题描述】:

我有以下角度代码,用于以选项卡式格式显示学生信息。 以下是 student.html 类

<div class="panel panel-default">
    <ul class="nav nav-tabs">
        <li ng-repeat="studentTab in studentList"
            ng-class="{active: selectedStudentTab.name === studentTab.name}">
            <a ng-click="selectStudentTab(studentTab)">{{studentTab.name}}</a>
        </li>
    </ul>
    <div class="panel-body">
        <div class="tab-content">
            <div class="tab-pane active" ng-repeat="studentTab in studentList"
                ng-show="selectedStudentTab.name === studentTab.name">
                <div style="padding: 35px; text-align: left;">
                    <div ng-repeat="project in studentTab.projects">
                    <p><pre>{{project.log}}</pre></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

在我的 studentController.js 中,我已将 studentList 的 oth 元素分配给选项卡,以下是代码:

$scope.init = function () {
    $rootScope.initOrRedirectToLandingPage($scope.loadStudents);
};

$scope.studentList = [];

$rootScope.loadStudents = function(){...
};

$scope.selectedStudentTab = $scope.studentList[0];

$scope.selectStudentTab = function (studentTab) {
        $scope.selectedStudentTab = studentTab;
};

此处的选项卡已正确创建,但默认情况下第一个选项卡在加载页面时未选中/处于活动状态,并且仅在单击事件时变为活动状态。 有人可以帮我弄清楚我错过了什么吗?任何帮助表示赞赏。

【问题讨论】:

  • selectedStudentTab 的值是多少?你的 JavaScript 看起来怎么样?

标签: html angularjs tabs nav


【解决方案1】:

在您加载 studentList 后,在控制器中将 selectedStudentTab 分配给列表中的第一项。

$scope.studentList = [...];
$scope.selectedStudentTab = $scope.studentList[0];

【讨论】:

  • 嗨迈克尔,我在我的控制器中做了同样的事情。我仍然看到默认情况下未选择第一个选项卡。
  • 你的 loadStudents 函数的内容是什么?它是否包含承诺?
  • 基本上,您需要确保在将值加载到列表后设置所选值。查看您的代码,您基本上将列表设置为一个空数组,然后从中选择第一项,但从未先将数据加载到数组中。您定义了加载列表的函数,但是在选择第一个项目之前没有调用它。
猜你喜欢
  • 1970-01-01
  • 2018-08-04
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
相关资源
最近更新 更多