将对象传递给AngularJS / bootstrap中的模态对话框

时间:2015-05-28 00:51:34

标签: javascript angularjs twitter-bootstrap angular-ui-bootstrap

我正在尝试使用下面的代码将对象传递到bootstrap / angularJS中的模态对话框中。我是按AngularJS UI Bootstrap modal is unable to perform functions from scope给出的答案的方式做到的。当模态表单应该从对editGroup()的调用打开时,我收到以下错误:

  

错误:[$ injector:unpr]未知提供者:selGroupProvider< - selGroup

var EditGroupModalController = function ($scope, $modalInstance, selGroup) {        
        $scope.user = $sessionStorage.user;
        $scope.selGroup = selGroup;

        $scope.closeModal = function () {
            $modalInstance.close();
        };
    };

    $scope.editGroup = function (selGroup) { // "selGroup" receives the current Group object from $scope.groupList[]
        $modal.open({
            templateUrl: 'app/views/administration/advanced/editgroup.html',
            controller:  ['$scope', '$modalInstance','$modal','$sessionStorage','advancedService','selGroup', EditGroupModalController],
            size: 'lg',
            windowTemplateUrl:'app/views/partials/modaltemplatedraggable.html',
            backdrop:'static',
            resolve: {
                item: function () {
                    return selGroup;
                }
            }
        });
    };

此错误的官方说明是here;但是,我不明白为什么我收到这个错误。对此的任何帮助都将非常赞赏。

2 个答案:

答案 0 :(得分:2)

  1. 您的控制器的依赖项列表与控制器的功能定义不匹配:在$modal.open中,您列出了六个依赖项,而在功能中只有三个存在。
  2. 依赖关系由resolve个密钥注入 - 在您的情况下,密钥为item
  3. 对代码进行必要的更改以使其正常工作:

    替换(1)

    var EditGroupModalController = function ($scope, $modalInstance, selGroup)
    

    var EditGroupModalController = function ($scope, $modalInstance, $modal, $sessionStorage, advancedService, selGroup)
    

    并替换(2)

    resolve: {
        item: function () {
    

    使用

    resolve: {
        selGroup: function () {
    

答案 1 :(得分:0)

您应该将解析中的变量命名为' selGroup'而不是' item'这样就可以正确注射。

此外,声明的依赖项的名称应与函数定义匹配。我把这个简单的演示放在一起。

angular.module('test', ['ui.bootstrap']).controller('TestController', function($scope, $modal) {
  var sel = {name: "A group"};
  var EditGroupModalController = function($scope, $modalInstance, selGroup) {
    $scope.selGroup = selGroup;
    $scope.closeModal = function() {
      $modalInstance.close();
    };
  };

  $scope.editGroup = function(selGroup) { // "selGroup" receives the current Group object from $scope.groupList[]
    $modal.open({
      template: '<div>Test {{selGroup.name}}</div>',
      controller: ['$scope', '$modalInstance', 'selGroup', EditGroupModalController],
      size: 'lg',
      backdrop: 'static',
      resolve: {
        selGroup: function() {
          return sel;
        }
      }
    });
  };
});

http://plnkr.co/edit/ec1PjkDZtf4yqINONnX5?p=preview