AngularJS:缩小我的指令

时间:2014-07-26 10:30:04

标签: angularjs angularjs-directive minify uglifyjs

我使用指令要求用户在模态中进行操作确认 它在开发过程中就像一个魅力,但是,在缩小之后,它就被打破了 这是我得到的可怕的“ $ injector:unpr ”错误:

Error: [$injector:unpr] Unknown provider: aProvider <- a
...

我认为问题是$scope$modalInstance被重命名,不应该,但我不知道如何避免这种情况......

这是指令代码:

'use strict';
app.directive('reallyClick', ['$modal', function($modal) {
  var modalInstanceCtrl = function ($scope, $modalInstance) {
    $scope.ok = function () {
      $modalInstance.close();
    };

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

  return {
    restrict: 'A',
    scope: {
      reallyClick: '&',
      item: '='
    },
    link: function (scope, element, attrs) {
      element.bind( 'click', function() {
        var message = attrs.reallyMessage || 'Are you sure?';
        var modalHtml = '<div class="modal-body">' + message + '</div>';
        modalHtml += '<div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">OK</button><button class="btn btn-warning" ng-click="cancel()">Cancel</button></div>';

        var modalInstance = $modal.open({
          template: modalHtml,
          controller: modalInstanceCtrl
        });

        modalInstance.result.then(function () {
          scope.reallyClick({item:scope.item}); // raise an error : $digest already in progress
        }, function() {
          // modal dismissed
        });
      });
    }
  };
}]);

我这样使用它:

...
<td title="Delete customer">
  <button
    class="btn btn-primary glyphicon glyphicon-trash"
    really-message="Are you really sure to remove customer <i>{{customer.name}}</i> ?" really-click="deleteCustomer(customerId)"
  ></button>
</td>
...

如果它可以提供任何帮助,这些是我在构建阶段使用的模块:

'auto_install',
'clean:dist',
'favicons',
'wiredep',
'useminPrepare',
'concurrent:dist',
'autoprefixer',
'concat',
'ngmin',
'copy:dist',
'cdnify',
'cssmin',
'uglify',
'filerev',
'usemin',
'htmlmin',

这些是我在我的app中注入的模块:

var app = angular.module('smallBusinessApp', [
  'ngSanitize',
  'ngRoute',
  'firebase',
  'ui.bootstrap',
]);

2 个答案:

答案 0 :(得分:5)

还需要使用依赖注入语法创建modalInstance Controller,

'use strict';
app.directive('reallyClick', ['$modal', function($modal) {    
  return {
    restrict: 'A',
    scope: {
      reallyClick: '&',
      item: '='
    },
    link: function (scope, element, attrs) {
      element.bind( 'click', function() {
        var message = attrs.reallyMessage || 'Are you sure?';
        var modalHtml = '<div class="modal-body">' + message + '</div>';
        modalHtml += '<div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">OK</button><button class="btn btn-warning" ng-click="cancel()">Cancel</button></div>';

        var modalInstance = $modal.open({
          template: modalHtml,
          controller: modalInstanceCtrl
        });

        modalInstance.result.then(function () {
          scope.reallyClick({item:scope.item}); // raise an error : $digest already in progress
        }, function() {
          // modal dismissed
        });
      });
    }
  };
}]);

ModelInstanceController:

app.controller('modalInstanceCtrl',['$scope','$modalInstance',function ($scope, $modalInstance) {
    $scope.ok = function () {
      $modalInstance.close();
    };

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

对我来说也是一个问题,不得不将模态的控制器部分分开并像这样做,希望它有所帮助!!

答案 1 :(得分:2)

我想这是你的modalInstanceCtrl。尝试不实现作为指令内的对象,但作为控制器:

app.controller('modalInstanceCtrl', [ '$scope', '$modalInstance',
function ($scope, $modalInstance) {
  $scope.ok = function () {
    $modalInstance.close();
  };

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

然后通过将其引用到引号中来引用它:

var modalInstance = $modal.open({
  template: modalHtml,
  controller: 'modalInstanceCtrl'
});