加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 不能在指令内使用$uibModal,但可以在控制器中访问

发布时间:2020-12-17 10:23:41 所属栏目:安全 来源:网络整理
导读:我想制定一个指令来打开一个模态.但$uibModal未在指令内定义. var app=angular.module("app",['ui.bootstrap']);app.controller('AppCtrl',function ($scope,$uibModal) { console.log("$uibModal controller",$uibModal);//getting object});app.directive(
我想制定一个指令来打开一个模态.但$uibModal未在指令内定义.
var app=angular.module("app",['ui.bootstrap']);

app.controller('AppCtrl',function ($scope,$uibModal) {
    console.log("$uibModal controller",$uibModal);//getting object
});

app.directive('showPopUp',function() {
    return {
        restrict: 'EA',link: function(scope,el,attrs,$uibModal) {
            console.log("$uibModal",$uibModal);//undefined here
                var modalInstance = $uibModal.open({
                  animation: $scope.animationsEnabled,templateUrl: 'popup.html',});

                modalInstance.result.then(function (selectedItem) {
                  scope.selected = selectedItem;
                },function () {
                });

        }  
    }
});

如何在我的指令中使用$uibModal来打开模态?

指令链接功能不可注入,它以固定顺序采用固定的参数集.但您可以将服务注入指令函数本身:
app.directive('showPopUp',function($uibModal) {
    return {
        restrict: 'EA',attrs) {

            var modalInstance = $uibModal.open({
                animation: scope.animationsEnabled,});

            modalInstance.result.then(function(selectedItem) {
                scope.selected = selectedItem;
            },function() {});

        }
    }
});

演示:http://plnkr.co/edit/jwNOM94DtyRIXTdhvJmy?p=preview

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读