javascript - 问题 : ng-repeat with specific view should be displayed on click

标签 javascript html angularjs

我有一个场景,其中有选项卡,当用户点击选项卡时,只有那个选项卡应该有星号,而不是每个选项卡

这是我的 html 代码:

<ul class="nav nav-tabs topics-div">
    <li class="li-div"><a><span>Topics</span> <span class="glyphicon glyphicon-play" aria-hidden="true"></span></a></li>
    <li ng-repeat="topics in oJdDetails.topics">
        <a style="cursor:pointer" ng-click="fngetQList(topics,$index)">
            <p>{{topics}}</p>
            <uib-rating ng-model="rate" ng-if="displayStars" max="max" read-only="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" titles="['one','two','three']" aria-labelledby="default-rating"></uib-rating>
        </a>
    </li>
</ul>

我有一个 Controller 代码如下:

$scope.fngetQList = function(topics, index) {
        debugger;
        $scope.displayQList = true;
        $scope.displayStars = true;
        $scope.sTopics = topics;
        $scope.index = index;
        getCandidateInterviewListService.fnGetQList(topics).then(function(response) {
            $scope.aQuestionList = response;
            console.log($scope.aQuestionList);
        });
    };

这里的 topics 是一个类似 ["html","css","angular"]

的数组

我在这里面临的问题是,当我单击一个选项卡时,所有选项卡都得到了星星,而我只需要为单击的选项卡加星

如有任何帮助,我们将不胜感激。

最佳答案

所有选项卡都获得了星标,因为您在 ng-repeat 中使用了相同的范围变量 displayStars。同样,由于相同的范围变量 rate,所有 topicsrate 都相同。

而是为它们中的每一个使用一个数组,如下所示(请注意,您需要知道数组 oJdDetails.topicslength。因此请确保您的初始化是正确的.),

HTML代码:

<ul class="nav nav-tabs topics-div">
    <li class="li-div"><a><span>Topics</span> <span class="glyphicon glyphicon-play" aria-hidden="true"></span></a></li>
    <!-- use `track by $index` here along with `ng-repeat` -->
    <li ng-repeat="topics in oJdDetails.topics track by $index">
        <a style="cursor:pointer" ng-click="fngetQList(topics, $index)">
            <p>{{topics}}</p>
            <uib-rating ng-model="rate[$index]" ng-if="displayStars[$index]" max="max" read-only="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" titles="['one','two','three']" aria-labelledby="default-rating"></uib-rating>
        </a>
    </li>
</ul>

Controller 代码:

// you need to know the `length` of `oJdDetails.topics` 
$scope.rates = new Arryay(oJdDetails.topics.length).fill(0); // initialisation of array with `0` value
$scope.displayStars = new Arryay(oJdDetails.topics.length).fill(false);; // initialisation of array with `false` value
$scope.fngetQList = function (topics, index) {
    $scope.displayQList = true;
    $scope.displayStars[index] = true;
    $scope.sTopics = topics;
    $scope.index = index;
    getCandidateInterviewListService.fnGetQList(topics).then(function(response) {
        $scope.aQuestionList = response;
        console.log($scope.aQuestionList);
    });
};

希望这能解决问题。

关于javascript - 问题 : ng-repeat with specific view should be displayed on click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38292690/

相关文章:

javascript - 浏览器中的半像素如何与 JavaScript clientWidth/clientHeight 关联?

javascript - NodeJs HTTP Post 调用,如何将其用作使用 $http 的 AngularJs 的 API

javascript - AngularJS - 将 Controller 重构为服务 - 理解 self/this 和范围

javascript - 无法读取函数之外的值

javascript - 在 Google Chrome 中打印 iFrame 时出现问题

javascript - 在javascript中为特定值循环遍历数组的错误值

php - 我可以结合元数据还是必须只使用一个元数据?

javascript - Firefox 4.06b IndexedDB 支持

javascript - React JS onDragCapture 与 onDrag

javascript - 递归读取Node JS中的目录