javascript - 具有相同 Controller 而不是新 Controller 的 Angular 引导 ui 模式

标签 javascript angularjs angular-ui-bootstrap

我正在使用 angular bootstrap ui 模态框,它说要为新 Controller 提供一个新的 $modalInstance。我想在初始化模态框的地方使用同一个 Controller 。我搜索了但没有成功。我找到了这个链接,但没有成功-

How to use the same controller for modal and non-modal form in Angular UI Bootstrap? Angular-ui bootstrap modal without creating new controller

app.controller('UserCtrl',['$scope','$filter','ngTableParams','$modal',function($scope, $filter, ngTableParams,$modal) {

  var modalInstance = $modal.open({
  templateUrl: 'myModalContent.html',
  controller: 'ModalInstanceCtrl', //Instead of this i want to use the same controller 'UserCtrl'
  size: size,
  resolve: {
    items: function () {
      return $scope.items;
    }
  }
});

modalInstance.result.then(function (selectedItem) {
  $scope.selected = selectedItem;
}, function () {
  $log.info('Modal dismissed at: ' + new Date());
});
 };
};

  } ]);

这样我就可以在同一个 Controller 上调用保存功能,这个 Controller 在保存点击模态框时被调用

最佳答案

在旧版本的 UI-Bootstrap 0.10.0 中是可能的 Download图书馆。即使是最新版本,它也适用于我

index.html

<!-- if you are using Bower -->    
<script src="bower_components/angular-bootstrap/ui-bootstrap.min.js">
</script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js">
</script>

<!-- button click opens modal-->
<button ng-click="openModal()">Button test</button>

<!-- modal -->
<!-- look at 'type' and 'id' values -->
<script type="text/ng-template" id="myTestModal.tmpl.html">
   <div class="modal-header">
      <h3>Modal Header</h3>
   </div>   

   <div class="modal-body">
      <p>Modal Body</p>
   </div>
 
   <div class="modal-footer">
      <button type="button" class="btn btn-default" ng-click="close()" data-dismiss="modal">Close
      </button>
      <button type="button" class="btn btn-primary" ng-click="doSomething()">Do Something
      </button>
   </div> 
</script>

modalDemoController.js

$scope.openModal=function(){
    $scope.modalInstance=$modal.open({
        templateUrl: 'myTestModal.tmpl.html',
        scope:$scope
    });
}

$scope.close=function(){
    $scope.modalInstance.dismiss();//$scope.modalInstance.close() also works I think
};

$scope.doSomething=function(){
    //any actions to take place
    console.log("Do Something");
}

关于javascript - 具有相同 Controller 而不是新 Controller 的 Angular 引导 ui 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25872280/

相关文章:

javascript - 像翻转一样水平旋转图像

javascript - 这个正则表达式是什么意思。检查 http 或 https 超链接就足够了吗?

javascript - Extjs 仅将单元格文本向右对齐

javascript - 如何在按钮单击时显示 html 内容,而不是使用 angularjs 创建像向导一样的新选项卡?

jquery - 将 CodeMirror 应用于 ng-model-bound 文本区域

html - 如果弹出模态对话框,则禁用 overflow-y

javascript - Object.defineProperty 的调用后/运行时性能影响

css - 使用 AngularJS 路由时的最小高度 css 问题

javascript - Angular UI/bootstrap typeahead 显示 'Error: No controller: ngModel' 错误

javascript - 使用 uib-accordion 的条件标题颜色