javascript - UI Bootstrap Modal Controller 的问题

标签 javascript angularjs

我是 Angular 新手。我创建了一个带有模板(panel.html)和 Controller (allPanelsRetrieved)的组件。当单击模板中定义的特定按钮时,将调用 Controller 中指定的 showDetails() 函数。此函数触发打开由模板 (panel-list.details-template.html) 和 Controller 指定的模式对话框,这两个模板都在 allPanelsRetrieved Controller 内定义。问题是显示了模式对话框,但 Controller 不起作用(单击“确定”按钮没有任何反应)。问题可能出在哪里?提前致谢

angular.
module('panelList')
.component('panelList', {
  templateUrl: '/panel-list/panel.html',
  controller: ['Panel', 'NgTableParams','$scope', '$location', '$uibModal',
   function PanelListController(Panel, NgTableParams, $scope, $location, $uibModal) {

  this.allPanelsRetrieved = (index, before) => {
  //..hidden code here  
  }

  this.showDetails = function () {
    $uibModal.open({
      templateUrl: '/panel-list/panel-list.details-template.html',
      controller: function ($uibModalInstance) {
        let $ctrl = this;
        $ctrl.ok = function () {
          $uibModalInstance.close();
        };
      },
    }).result.then(
      function (success) {
        alert(success);
      },
      function (error) {
        alert(error);
      }
    );
  };
 }]
});

这是模板 panel-list.details-template.html:

<div>
<script type="text/ng-template" id="/panel-list/panel-list.details-template.html">
        <div class="modal-header">
            <h3 class="modal-title">Modal title</h3>
        </div>
        <div class="modal-body">
            Modal content
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="$ctrl.ok()">Close</button>
        </div>
</script>

最佳答案

使用$scope使其以AngularJS的方式工作。这是working plnkr demo您试图实现的目标。

$uibModal.open({
    templateUrl: '/panel-list/panel-list.details-template.html',
    controller: function ($uibModalInstance, $scope) {
        $scope.ok = function () {
            $uibModalInstance.close();
        };
    }
}).result.then(
    function (success) {
        alert(success);
    },
    function (error) {
        alert(error);
    }
);

查看

<script type="text/ng-template" id="/panel-list/panel-list.details-template.html">
  <div class="modal-header">
    <h3 class="modal-title">Modal title</h3>
  </div>
  <div class="modal-body">
    Modal content
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()">Close</button>
  </div>
</script>

关于javascript - UI Bootstrap Modal Controller 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613721/

相关文章:

javascript - 链接可以在浏览器中使用并提供 JSON,但不能在 Angular 中使用

angularjs - 如何模拟 get(id) 请求

javascript - 将javascript变量传递给 Angular js中的ng-click事件

javascript - 比较 JavaScript 和 AngularJS?

javascript - 如何随机选择三张图像,然后用新图像将它们淡出?

javascript - 提高 Instafeed 的 Feed 请求限制

javascript - 如何使两个对象相交并在 javascript 中保留第一个对象的引用

javascript - 显示 JSON 数据中的图像?

javascript - 将值增加 1,直到使用 Angular 鼠标事件释放鼠标

javascript - webpack 开发和生产构建模式有什么区别?