Angular Boostrap模态服务 [英] Angular Boostrap Modal Service

查看:62
本文介绍了Angular Boostrap模态服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击Angular UI网格上的编辑按钮时,我正在尝试打开模式.我是Angular的新手,仍然在学习.我已经进行了一些搜索,但是仍然无法解决如何将我的模态实例注入StaffController的情况,因此可以使用它.真的很感谢在此方面的任何建议/帮助.谢谢

I am attempting to open a modal, when clicking the edit button on an Angular UI Grid. I am new to Angular and still learning. I have done some searching but still cannot work out how to inject my modal instance into the StaffController, so it can be used. Will really appreciate any advice / help on this. Thanks,

我正在使用角度模态示例: https://angular-ui.github.io/bootstrap/

I am using the angular modal example off: https://angular-ui.github.io/bootstrap/

索引文件(包含网格和示例模式按钮):

Index File(Contains the Grid and example Modal Buttons):

@*Staff Grid*@
<body ng-app="Application">
<div ng-controller="ApiStaffController">
    <div class="table-responsive angular-grid" ui-grid="gridOptions" ui-
grid-move-columns ui-grid-resize-columns ui-grid-exporter ui-grid-selection>
    </div>
</div>

<div ng-controller="ModalDemoCtrl as $ctrl" class="modal-demo">
    <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title" id="modal-title">Create User</h3>
        </div>
        <div class="modal-body" id="modal-body">
            <label asp-for="Person.Forename" class="control-label"></label>
            <input type="text" class="form-control" ng-model="$ctrl.newUser.Forename" />
            <label asp-for="Person.Surname" class="control-label"></label>
            <input type="text" class="form-control" ng-model="$ctrl.newUser.Surname" />
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()" ng-disabled="!$ctrl.newUser.Forename || !$ctrl.newUser.Surname">OK</button>
            <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button>
        </div>
    </script>

    <script type="text/ng-template" id="stackedModal.html">
        <div class="modal-header">
            <h3 class="modal-title" id="modal-title-{{name}}">The {{name}} modal!</h3>
        </div>
        <div class="modal-body" id="modal-body-{{name}}">
            Having multiple modals open at once is probably bad UX but it's technically possible.
        </div>
    </script>


    <button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open me!</button>
    <button type="button" class="btn btn-default" ng-click="$ctrl.open('lg')">Large modal</button>
    <button type="button" class="btn btn-default" ng-click="$ctrl.open('sm')">Small modal</button>
    <button type="button" class="btn btn-default" ng-click="$ctrl.open('sm', '.modal-parent')">
        Modal appended to a custom parent
    </button>
    <button type="button" class="btn btn-default" ng-click="$ctrl.toggleAnimation()">Toggle Animation ({{ $ctrl.animationsEnabled }})</button>
    <button type="button" class="btn btn-default" ng-click="$ctrl.openComponentModal()">Open a component modal!</button>
    <button type="button" class="btn btn-default" ng-click="$ctrl.openMultipleModals()">
        Open multiple modals at once
    </button>
    <div ng-show="$ctrl.selected">Selection from a modal: {{ $ctrl.selected }}</div>
    <div class="modal-parent">
    </div>
</div>

我有一个名为:ModalController的文件,其中包含以下内容:(来自示例)

I have a file named: ModalController that contains the below: (This is from the example)

angular.module('Application').controller('ModalDemoCtrl', function 
($uibModal, $log, $document) {
var $ctrl = this;
$ctrl.items = ['item1', 'item2', 'item3'];

$ctrl.animationsEnabled = true;

$ctrl.open = function (size, parentSelector) {
    var parentElem = parentSelector ?
        angular.element($document[0].querySelector('.modal-demo ' + 
parentSelector)) : undefined;
    var modalInstance = $uibModal.open({
        animation: $ctrl.animationsEnabled,
        ariaLabelledBy: 'modal-title',
        ariaDescribedBy: 'modal-body',
        templateUrl: 'myModalContent.html',
        controller: 'ModalInstanceCtrl',
        controllerAs: '$ctrl',
        size: size,
        appendTo: parentElem,
        resolve: {
            items: function () {
                return $ctrl.items;
            }
        }
    });

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

$ctrl.openComponentModal = function () {
    var modalInstance = $uibModal.open({
        animation: $ctrl.animationsEnabled,
        component: 'modalComponent',
        resolve: {
            items: function () {
                return $ctrl.items;
            }
        }
    });

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

$ctrl.openMultipleModals = function () {
    $uibModal.open({
        animation: $ctrl.animationsEnabled,
        ariaLabelledBy: 'modal-title-bottom',
        ariaDescribedBy: 'modal-body-bottom',
        templateUrl: 'stackedModal.html',
        size: 'sm',
        controller: function ($scope) {
            $scope.name = 'bottom';
        }
    });

    $uibModal.open({
        animation: $ctrl.animationsEnabled,
        ariaLabelledBy: 'modal-title-top',
        ariaDescribedBy: 'modal-body-top',
        templateUrl: 'stackedModal.html',
        size: 'sm',
        controller: function ($scope) {
            $scope.name = 'top';
        }
    });
};

$ctrl.toggleAnimation = function () {
    $ctrl.animationsEnabled = !$ctrl.animationsEnabled;
};
});

//请注意,$ uibModalInstance表示模式窗口(实例)依赖项.//与上面使用的$ uibModal服务不同.

// Please note that $uibModalInstance represents a modal window (instance) dependency. // It is not the same as the $uibModal service used above.

angular.module('Application').controller('ModalInstanceCtrl', function 
($uibModalInstance, StaffService, items) {
var $ctrl = this;
$ctrl.items = items;
$ctrl.selected = {
    item: $ctrl.items[0]
};

$ctrl.ok = function () {
    alert($ctrl.newUser.Forename);
    alert($ctrl.newUser.Surname);
    //$uibModalInstance.close($ctrl.selected.item); //Untill we do the saving staff
};

$ctrl.cancel = function () {
    $uibModalInstance.dismiss('cancel');
};
});

//请注意,关闭和关闭绑定来自$ uibModalInstance.

// Please note that the close and dismiss bindings are from $uibModalInstance.

angular.module('Application').component('modalComponent', {
templateUrl: 'myModalContent.html',
bindings: {
    resolve: '<',
    close: '&',
    dismiss: '&'
},
controller: function () {
    var $ctrl = this;

    $ctrl.$onInit = function () {
        $ctrl.items = $ctrl.resolve.items;
        $ctrl.selected = {
            item: $ctrl.items[0]
        };
    };

    $ctrl.ok = function () {
        $ctrl.close({ $value: $ctrl.selected.item });
    };

    $ctrl.cancel = function () {
        $ctrl.dismiss({ $value: 'cancel' });
    };
}
});`

我这里有一个职员控制器,该控制器从API调用返回数据,以填充UI网格.

I have a staff controller here that returns data from an API call, to populate the UI grid.`

var editButtonTemplate = '<div class="ui-grid-cell-contents"><button type= 
"button" class="btn btn-xs btn-primary" ng-
click="grid.appScope.vm.editRow(grid, row)" <i class="fa fa-edit"></i>
</button></div>'

/*Staff Grid*/
app.controller('ApiStaffController', function ($scope, StaffService) {

$scope.gridOptions = {
    data: 'Data',
    enableFiltering: true,
    showGroupPanel: true,
    enableGridMenu: true,
    resizable: true,
    enableColumnResizing: true,
    enableRowSelection: true,
    enableRowHeaderSelection: false,
    showGridFooter: true,
    noUnselect: true,
    multiSelect: false,
    modifierKeysToMultiSelect: false,
    noUnselect: true,
    columnDefs: [{ field: 'person.forename', displayName: 'Forename' },
    { field: 'person.surname', displayName: 'Surname' },
    { field: 'person.dateofbirth', displayName: 'DOB' },
    { field: 'employmentStartDate', displayName: 'Employment Start Date' },
    { field: 'employmentEndDate', displayName: 'Employment End Date' },
    { name: 'edit', displayName: 'Edit', cellTemplate: editButtonTemplate },
    { name: 'delete', displayName: 'Delete', cellTemplate: '<button id="deleteBtn" type="button" class="btn-xs btn-danger fa fa-1x fa-trash-o" ng-click="$parent.$parent.deleteTeam(row.entity)"></button> ' }]
};

/*Gets all the staff records using the service*/
GetAllStaff();
function GetAllStaff() {
    var promiseGet = StaffService.getAllStaff();
    promiseGet.then(function (pl) { $scope.Staff = pl.data, $scope.Data = pl.data },
        function (errorPl) {
            $log.error('Error Getting Records.', errorPl);
        });
}
});

推荐答案

我设法通过将$ uibModal注入人员控制器来解决此问题,如下所示: app.controller('ApiStaffController',函数($ scope,StaffService,$ uibModal)

I managed to resolve this by injecting: $uibModal into the staff controller like below: app.controller('ApiStaffController', function ($scope, StaffService, $uibModal)

然后通过显示如下模态来对其进行测试:

Then testing it by displaying the modal like this:

  var modalInstance = $uibModal.open({
    templateUrl: 'myModalContent.html',
    scope: $scope, //passed current scope to the modal
    size: 'lg'
});

如果有更好/更灵活的方法,我感谢所有反馈,谢谢.

If there is a better / more flexible way of doing this I appreciate all feedback, thanks.

这篇关于Angular Boostrap模态服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆