简体   繁体   English

AngularJS在单击按钮时动态添加li元素

[英]Angularjs dynamically add li element on button click

Im trying to dynamically add an li element on each button click. 我试图在每次单击按钮时动态添加li元素。 But the new li element is not getting created. 但是没有创建新的li元素。 Im trying to use ng-repeat to acheive the same. 我试图使用ng-repeat达到相同的效果。 Below is my code 下面是我的代码

html code 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>

Angular js code Angular js代码

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++ ;

 }
 }
 }); 

In the above code, on click of the Query button, the first time, the tab gets created. 在上面的代码中,第一次单击“查询”按钮时,将创建选项卡。 On the second click, the same tab gets modified instead of creating a new tab. 第二次单击时,将修改同一选项卡,而不是创建一个新选项卡。 Can you please let me know how to acheive the same. 您能不能让我知道如何达到相同的效果。 on each button click, I want a new tab to be created. 在每次单击按钮时,我希望创建一个新选项卡。

Any help on this is much appreciated. 任何帮助对此表示感谢。

You are doing ng-repeat="tabs in tabcount" while tabcount is a number, ngRepeat needs to iterate over an iterable variable, such as list or an object. tabcount是一个数字时,您正在执行ng-repeat="tabs in tabcount" ,ngRepeat需要遍历可迭代变量(例如列表或对象)。

From the docs 来自文档

The ngRepeat directive instantiates a template once per item from a collection. ngRepeat指令对集合中的每个项目实例化一次模板。 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. 每个模板实例都有其自己的作用域,其中给定的循环变量设置为当前集合项,$ index设置为项索引或键。

Try to instantiate tabcount as an empty array 尝试将tabcount实例tabcount一个空数组

$scope.tabcount = [];

Inside the onClick function push objects like this 在onClick函数内部,推入这样的对象

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

And iterate on that list in the html using ngRepeat 并使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM