如何在指令范围内为 $modal 设置“内联"控制器? [英] How to set 'inline' controller for $modal in a directive's scope?

查看:28
本文介绍了如何在指令范围内为 $modal 设置“内联"控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在指令中打开 Bootstrap UI $modal.它可以工作,但是当我想添加一个内联"控制器时(请参阅下面代码中的注释行),我收到一个[$injector:unpr] Unknown provider: nProvider <- n"错误.

谁能告诉我为什么?

angular.module('myApp.directives').directive('eModalForm', ['$modal', function ($modal) {返回 {限制:'A',链接:函数($scope,$element,$attrs){$scope.openModal = 函数 () {console.log('//==//');var modalInstance = $modal.open({templateUrl: '/App/Templates/modal.html',//注释这些行以防止错误://控制器:函数($scope,$modalInstance){//$scope.$modalInstance = $modalInstance;//},尺寸:'lg'});};}};}]);

我是这样调用这个函数的:

解决方案

控制器依赖在缩小过程中丢失,只需使用扩展语法:

controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {...}],...

I'm trying to open a Bootstrap UI $modal inside a directive. It works, but when I want to add an 'inline' controller (see commented lines in the code below), I get a "[$injector:unpr] Unknown provider: nProvider <- n" error.

Can anyone tell me why?

angular.module('myApp.directives').directive('eModalForm', ['$modal', function ($modal) {

   return {
      restrict: 'A',
      link: function ($scope, $element, $attrs) {

         $scope.openModal = function () {
            console.log('//==//');
            var modalInstance = $modal.open({

                templateUrl: '/App/Templates/modal.html',

                // commented these lines to prevent error:
                //controller: function ($scope, $modalInstance) {
                //  $scope.$modalInstance = $modalInstance;
                //},

                size: 'lg'

            });

         };
      }

   };

}]);

I am calling this function like this:

<div ng-repeat="item in list" ng-click="openModal(item)" e-modal-form>

解决方案

The controller dependencies are lost during minification, just use the extended syntax:

controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
    ...
}],
...

这篇关于如何在指令范围内为 $modal 设置“内联"控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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