Angular UI - 在我的服务DI中使用$ modal使得$ modal为undefined

时间:2014-01-10 00:46:58

标签: angularjs angular-ui-bootstrap

在最新的AngularJS和UI-Bootstrap中,我的下面的代码注入了$ modal,但它是未定义的。请注意,如果我将$ modal注入控制器,它的效果很好。

有人可以建议为什么DI无法在我的以下服务中启动$ modal吗?

angular.module ( 'geRbAuthenticationModule', ['ui.bootstrap'])
.factory ('geRbAuthenticationService',  // service name
  [
    '$timeout',             // dependencies
    '$http',
    '$q',
    '$modal',
    function ( $scope, $timeout, $http, $q, $modal  ) {

      BUG here:  $modal is UNDEFINED

     '
     '
     '

谢谢。

1 个答案:

答案 0 :(得分:1)

您在字符串列表中缺少字符串“$ scope”依赖项。所以改变:

['$timeout','$http','$q','$modal',
function ( $scope, $timeout, $http, $q, $modal  ) {

到:

['$scope','$timeout','$http','$q','$modal',
function ( $scope, $timeout, $http, $q, $modal  ) {

$modal失败的原因是因为它是列表中的最后一个。现在超时进入范围,http进入超时等,直到未定义进入$ modal。

这是一个容易犯的错误,使得一些人从他们的代码中删除字符串列表并在他们的grunt脚本中使用ngminngmin然后自动处理此表单要解决的缩小器问题。这使您不必担心将字符串名称与参数匹配:

.factory ('geRbAuthenticationService',  // service name
    function ( $scope, $timeout, $http, $q, $modal  ) {