AngularJS事件在弹出窗口中动态添加按钮

时间:2017-09-25 20:33:47

标签: angularjs events popup

我对angularJs有疑问。 打开弹出窗口并在弹出窗口中动态添加按钮后,我不知道如何触发按钮事件。 我几乎尝试了'一切'。

以下是一个示例:https://plnkr.co/edit/QfnDttJE2OnfHzt65tBQ?p = preview

1 个答案:

答案 0 :(得分:0)

好的,鉴于你的代码,它可以改善一点,但这应该是坚实的解决方案: https://plnkr.co/edit/UiOsyHBjVZTW33yr6h3z?p=preview

使用Javascript:

app.controller('ModalInstanceCtrl', function ($uibModalInstance,$compile) {
      var $ctrl = this;
      $ctrl.buttonArray = [];

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

      $ctrl.add2 = function(){        
       $ctrl.buttonArray.push('message' + $ctrl.buttonArray.length)  
        };

      $ctrl.message = function () {
        alert('Message');
      };

});

HTML:

    <div ng-app="app" ng-controller="postoviCtrl as $ctrl">

      <script type="text/ng-template" id="modalOdabraniPost.html">

          <div class="modal-body">
                        <p>Header</p>
                        <hr/>
                        <button class="btn btn-sm" type="button" ng-click="$ctrl.add2()">Add</button>

                        <div id="content">
                          <button ng-repeat="btn in $ctrl.buttonArray" class="btn btn-primary btn-sm" type="button" ng-click="$ctrl.message()">{{btn}}</button>'
                        </div>
          </div>
            <div class="modal-footer">
                <button class="btn btn btn-primary" type="button" ng-click="$ctrl.cancel2()">Close</button>

            </div>
        </script>

        <a href="" class="label label-danger" ng-click="$ctrl.open2()">Open</a> 

    </div>