javascript - Angular js : Dynamically created tab does not get active/selected

标签 javascript angularjs angular-bootstrap

我有下面的 angularjs 代码,它通过按新按钮来创建选项卡。但新创建的选项卡在创建后不会被激活或选择。始终是最后一个活跃之前的一个! 任何人都知道出了什么问题吗?

plnkr:https://plnkr.co/edit/1329tgGonObRQ6Drk75A?p=preview

HTML:

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
      <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.5.js"></script>
    <script src="example.js"></script>
         <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">



  </head>
  <body>

<div ng-controller="TabsParentController">


   <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title">I'm a modal!</h3>
        </div>
        <div class="modal-body">
            <ul>
               Sure to delete?
            </ul>
            Selected: <b>{{ selected.item }}</b>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
            <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
        </div>
    </script>


    <uib-tabset active="active">

        <uib-tab ng-repeat="workspace in workspaces"
             heading="{{workspace.name}}"
             >
            <div ng-controller="TabsChildController">
                <div>
                    {{$parent.workspace.id}} : {{ $parent.workspace.name}}
                </div>
                <input type="text" ng-model="workspace.name"/>
                 <button class="btn" type="button" ng-click="open('sm',workspace)">Delete</button>
            </div>     
        </uib-tab>
      <uib-tab index="0" select="addWorkspace()">
              <uib-tab-heading>
                <i class="glyphicon glyphicon-plus"></i>
            </uib-tab-heading>
        </uib-tab>
    </uib-tabset>
</div>
  </body>
</html>

Javascript:

var app = angular.module('plunker', ['ngAnimate','ui.bootstrap']);

app.controller("TabsParentController", function ($scope,$uibModal) {
  $scope.animationsEnabled = true;

  $scope.open = function (size, workspace) {

    var modalInstance = $uibModal.open({
      animation: $scope.animationsEnabled,
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {

         workspace: function () {
          return workspace;
        }

      }
    });

    modalInstance.result.then(function (selectedItem) {
       var index=$scope.workspaces.indexOf(selectedItem)
      $scope.workspaces.splice(index,1);     
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };



    var setAllInactive = function() {
         //angular.forEach($scope.workspaces, function(workspace) {
           // workspace.active = false;
   // });
    };
    var addNewWorkspace = function() {

        var id = $scope.workspaces.length+1 ;

        $scope.workspaces.push({
            id: id,

            name: "Workspace " + id,

        });
        $scope.active=$scope.workspaces.length -1;


    };

    $scope.workspaces =
    [

    ];

    $scope.addWorkspace = function () {
        setAllInactive();
        addNewWorkspace();

    };    


     $scope.remove=function(item){ 
      var index=$scope.workspaces.indexOf(item)
      $scope.workspaces.splice(index,1);     
    }

});

angular.module('plunker').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, workspace) {

  $scope.selectedworkspace = workspace;

  $scope.ok = function () {
    $uibModalInstance.close( $scope.selectedworkspace );
  };

  $scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
});




app.controller ("TabsChildController", function($scope, $log){

});

最佳答案

如果您希望最新的选项卡处于事件状态,您将需要设置

$scope.active = $scope.workspaces.length;

但另一个问题是,当您推送新的工作区时。该指令需要一些时间来重新渲染 DOM 并准备好所有范围变量。因此,在推送后,如果您尝试将最新的选项卡指定为事件选项卡,将会导致错误。

因此,为了快速证明我的观点(没有最正确的方法),请尝试这个并且您的代码将起作用。请记住将 $timeout 作为依赖项注入(inject)

app.controller("TabsParentController", 
    function ($scope,$uibModal, $timeout) {


        ....
        ....

        $scope.workspaces.push({
            id: id,
            name: "Workspace " + id,
        });

        //introduce a 50 ms delay before setting the active tab
        $timeout(function(){
           $scope.active = $scope.workspaces.length;
        }, 50);

        ....
        ....

    }
);

see it in plunker

关于javascript - Angular js : Dynamically created tab does not get active/selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162634/

相关文章:

javascript - 在循环中自动执行函数

javascript - Bootstrap日期选择器键盘导航: select date when "tabbing out"

javascript - 为什么 Angular js 中的 $watch 会导致 console.log 输出两次?

css - 使用 Angular Bootstrap 版本 0.13.4 在面板上制作 "Pressed"效果

javascript - 如何在预先输入的 Angular 输入搜索框中分离两组数据?

javascript - 类型 'Promise<User>' Angular 6 上不存在“订阅”

javascript - 将键和值对分成两个数组

node.js - Protractor JS 预处理器

javascript - angularjs:ng-repeat 中的自定义过滤器

angular - 单击事件不适用于带有 ngbButton 的 Angular 4 Bootstrap 中的输入类型 ="radio"