6

在我的 angularjs 应用程序中,我使用 UI Bootstrap 来创建模态。我将范围和自定义控制器传递到模态中,它显示了来自原始范围的数据,但无法执行其任何功能。我有主控制器:

myapp.controller('WsCtrl', function WsCtrl($scope, $location, todoStorage, filterFilter, $modal, $log) {

在控制器中,我有下一个:

$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
    var modalInstance = $modal.open({
        templateUrl: 'partials/users.html',
        scope: $scope,
        controller: ModalInstanceCtrl,
        resolve: {
            items: function () {
                return $scope.items;
            },
            users: function(){
                return $scope.users;
            },
            CurrentDate: function(){
                return $scope.CurrentDate;
            }
        }
    });

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

而且我在控制器之外还有另一个功能:

var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

    $scope.items = items;
    $scope.users = users;
    $scope.CurrentDate = CurrentDate;
    $scope.selected = {
        item: $scope.items[0]
    };
    $scope.num = 11;

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

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

当我将范围传递给模态时 - 我可以看到我的所有用户,但我无法添加一个,因为我的原始范围内的功能出现问题。

4

2 回答 2

22

你不需要scope: $scope。该resolve参数负责将变量传递给 ModalInstanceCtrl。但是您必须将这些参数添加到其依赖项中(它们的名称必须与 from 匹配resolve),所以如果您有:

resolve: {
    foo: function(){
        return $scope.something
    }
}

那么你一定有

var ModalInstanceCtrl = function ($scope, $modalInstance, foo) {
    $scope.foo = foo;
    // ...
}

哦,函数可以像其他变量一样在内部传递resolve

resolve: {
    someFunction: function(){
        return $scope.someFunctionFromOriginalScope
    }
}

此外,您可以在该部分中注入任何其他服务并在其中resolve执行其他逻辑:

resolve: {
    someFunction: function(configService){
        if (configService.isProduction){
            return angular.noop;
        } else {
            return $scope.someFunctionFromOriginalScope;
        }
    }
}
于 2013-10-01T16:03:21.420 回答
10

如果您确实需要传递自定义范围(即,如果您想避免不总是满足的依赖注入),您可以这样做:

$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
    var modalInstance = $modal.open({
        templateUrl: 'partials/users.html',
        scope: function() {
            var scope = $rootScope.$new();
            scope.items = $scope.items;
            scope.users = $scope.users;
            scope.currentDate = $scope.currentDate;
            scope.someFunction = function () {
                // do some stuff with scope.items
            }
            return scope;
        }(),
        controller: 'ModalInstanceCtrl'
    });

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

你的控制器看起来像这样:

var ModalInstanceCtrl = function ($scope, $modalInstance) {
    // Your controller already has $scope.items, $scope.users, $scope.currentDate and $scope.someFunction

    $scope.selected = {
        item: $scope.items[0]
    };
    $scope.num = 11;

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

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
};
于 2014-02-18T19:36:29.983 回答