模态在指令中返回未定义

Modal is coming back undefined in directive

Google 给我错误:"TypeError: Cannot read property 'open' of undefined" 以响应我的 ui-bootstrap 模块。我一直在使用其他 ui-bootsrap 指令。

我没有正确声明模态依赖关系吗?

angular.module('ireg').directive('study', function (studyFactory) {
return {
    restrict:'E',
    scope: {             
        objectid:'@objectid'
    },
    templateUrl: '/ireg/components/study/study.html',

    link: function (scope, element, attrs, $modal) {

        scope.openMilestonesDialog = function () {
            var modalInstance = $modal.open({
                animation: $scope.animationsEnabled,
                templateUrl: '/ireg/components/add-milestones/addMilestonesDialog.html',
                controller: '/ireg/components/add-milestones/addMilestonesDialogController.js',
                size: lg
            });
        };

    }//end of link

}
});

angular.module('ireg').controller('addMilestonesDialogController', function ($scope, $modalInstance, studyId) {

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


});

您应该在 directive 函数中包含 $modal 服务,而不是 link 函数:

angular.module('ireg').directive('study', function (studyFactory, $modal) {
    return {
        restrict:'E',
        scope: {             
            objectid:'@objectid'
        },
        templateUrl: '/ireg/components/study/study.html',

        link: function (scope, element, attrs) {

            scope.openMilestonesDialog = function () {
                var modalInstance = $modal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: '/ireg/components/add-milestones/addMilestonesDialog.html',
                    controller: '/ireg/components/add-milestones/addMilestonesDialogController.js',
                    size: 'lg'
                });
            };

        }//end of link

    }

});

是的,阿尔贝托 I.N.J。是的,你应该将 size 属性设置为字符串。

您应该将 $model 注入指令本身并将 lg 更改为 'lg'

angular.module('ireg').directive('study', function (studyFactory, $modal) {
    return {
        restrict:'E',
        scope: {             
            objectid:'@objectid'
        },
        templateUrl: '/ireg/components/study/study.html',

        link: function (scope, element, attrs) {

            scope.openMilestonesDialog = function () {
                var modalInstance = $modal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: '/ireg/components/add-milestones/addMilestonesDialog.html',
                    controller: '/ireg/components/add-milestones/addMilestonesDialogController.js',
                    size: 'lg'
                });
            };

        }//end of link

    }

});