javascript - Angularjs 在按钮单击时动态添加 li 元素

标签 javascript html angularjs

我尝试在每次单击按钮时动态添加 li 元素。但是新的 li 元素没有被创建。我正在尝试使用 ng-repeat 来实现相同的目的。 下面是我的代码

html代码

<div data-ng-app="myApp" data-ng-controller="TestController" >
<div ng-show="showQuerydiv" id="qscompid" style="margin-left:170px;margin-top:90px">
    <div class="btn-group" id="buttonOptions" style="width: 100%; position:absolute;">
    <a href="#" class="btn btn-primary" ng-click="openQuerydiv('query')">Query</a>
    <a href="#" class="btn btn-primary" ng-click="openQuerydiv('script')">Script</a>
    <a href="#" class="btn btn-primary" ng-click="openQuerydiv('compare')">Comp</a>
    <div style="float: right; width: 30%">
    <a href="#operationDetailInfo" class="glyphicon glyphicon-info-sign"  data-toggle="collapse" style="float: left;" title="Info"></a>
    <div  id="operationDetailInfo" class="collapse" style="width: 95%; float: left;">
    </div>
    </div>
    <br>
    <div id="sqlQueryDiv" ng-show="isShow" style="width: 100%;margin-top: 30px;margin-left: -170px;">
        <ul class="nav nav-tabs" role="tablist" id="queryULId" style="width: 1140px;height: 39px;">
            <li class="active" ng-repeat="tabs in tabcount">
                <a href="#queryTab"+{{tabCount}} role="tab" data-toggle="tab">
                {{tabName}}{{tabCount}}
                </a>
                <span  class="close" style="font-size: 12px; position: absolute; margin-left: 85%; margin-top: -25%; cursor: pointer;">X</span>
        </li>
    </ul>
    <div class="tab-content" style="width:1177px;height: 225px;">

    </div> 
    </div>
    </div>
</div>

AngularJS代码

var app = angular.module('myApp', []);
app.controller('TestController', function($scope){
console.log('Going for execution ');
$scope.tabCount = 0 ;
$scope.showQuerydiv = true;
$scope.isShow = false;
$scope.list =" " ;
$scope.openQuerydiv = function(parameter)
{
    alert("inside the openqueryDiv") ;
    if(parameter == 'query')
{
     alert("inside the openquerydiv" + parameter);
     $scope.isShow=true;
     $scope.tabName='SQL Query';
     $scope.tabCount++ ;

 }
 }
 }); 

在上面的代码中,第一次单击“查询”按钮时,会创建选项卡。第二次单击时,同一选项卡将被修改,而不是创建新选项卡。您能否让我知道如何实现同样的目标。单击每个按钮时,我想创建一个新选项卡。

非常感谢对此的任何帮助。

最佳答案

您正在执行ng-repeat="tabs in tabcount",而tabcount是一个数字,ngRepeat需要迭代可迭代变量,例如列表或对象。

来自docs

The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index is set to the item index or key.

尝试将 tabcount 实例化为空数组

$scope.tabcount = [];

在 onClick 函数内部推送像这样的对象

$scope.openQuerydiv = function(parameter) {
    $scope.tabcount.push({
         type: parameter,
         link: "some_link.html",
         name: "some name"
    });
}

并使用 ngRepeat 迭代 html 中的列表

<li class="active" ng-repeat="tabs in tabcount">
     <a href="{{tabs.link}}" role="tab" data-toggle="tab">
     {{tabs.name}}
     </a>
     <span  class="close" style="font-size: 12px; position: absolute; margin-left: 85%; margin-top: -25%; cursor: pointer;">X</span>
</li>

关于javascript - Angularjs 在按钮单击时动态添加 li 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42185759/

相关文章:

javascript - 如何显示来自函数装饰器的消息?

javascript - 在模糊事件中隐藏日期选择器

javascript - 如何创建一个函数来检查数组 javascript 中的键值

android - 在 Android 上将 HTML 转换为图像(任何格式)

c# - 通过标签在 C# 中拆分文本

javascript - Angular-Service Array.filter 与基本 for 循环

javascript - 将参数传递给 angularjs $q 中 promise 的成功回调

JavaScript Promise 封装在函数中还是裸露的?

javascript - 使用 JavaScript 提示获取输入并将其传递

javascript - 使用 AngularJS 和 OpenWeather 创建天气应用程序