2015-02-05 54 views
0

我試圖有一個簡單的模式出現時,用戶點擊我的演示應用程序仍在建設中的東西。試圖解僱一個簡單的角模態導致:無法讀取屬性'解僱'的未定義

一切工作到我希望用戶點擊模式上的'關閉'按鈕。當他們這樣做,他們得到:

TypeError: Cannot read property 'dismiss' of undefined

這是我在我的主控制器:

$scope.underConstruction = function() { 
    var modalInstance = $modal.open({ 
     templateUrl: 'app/shared/underconstruction.html', 
     controller: 'ModalInstanceCtrl', 
     size: 'sm', 
     resolve: { 
      '$modalInstance': function() { return function() { return modalInstance; } } 
     } 
    }); 
    console.log('modal opened'); 
    modalInstance.result.then(function (response) { 
     $scope.selected = response; 
     console.log(response); 
    }, function() { 
     console.log('Modal dismissed at: ' + new Date()); 
    }); 
}; 

然後在我的ModalInstanceCtrl我:

app.controller('ModalInstanceCtrl', ['$scope', '$modal', function ($scope, $modal, $modalInstance) { 
$scope.cancel = function() { 
    $modalInstance.dismiss('cancel'); 
}; 
}]); 

我使用angular- UI版本0.12.0,angularjs版本v1.3.11

命中close()方法,然後引發上述錯誤。

我環顧了各種結果和問題,看到了有關錯誤和其他問題的參考,但用例比我的更復雜 - 我的模式只顯示一些文本和一個關閉按鈕。例如,我found an answer that says

$modalInstance is made available for injection in the controller by AngularUI Bootstrap implementation. So, we don't need any effort ro "resolve" or make it available somehow.

+1

Stewie正確回答。你應該擺脫這種表示法,並且在縮小腳本時使用* ngmin *或* ngannotate *。 – Ioan 2015-02-05 14:38:43

回答

2

您沒有將您的$modalInstance參數聲明爲undefined,因爲它沒有將您的內聯註釋數組聲明爲依賴項,並聲明ModalInstanceCtrl控制器。

它應該是:

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

的「我們不需要任何努力...」的一部分,並不意味着你不必指定它作爲一個依賴。

+0

啊,好的。謝謝。這更像我現在所期待的。 – 2015-02-05 14:44:11

3

我能夠把事情簡單化:在我的模式模板

$scope.underConstruction = function() { 
    var modalInstance = $modal.open({ 
     templateUrl: 'app/shared/underconstruction.html' 
    }); 
    console.log('modal opened'); 
}; 

然後:

<button class="btn btn-primary" ng-click="$dismiss('cancel')">Close this message</button> 

按照文檔:

In addition the scope associated with modal's content is augmented with 2 methods:

$close(result)

$dismiss(reason)

Those methods make it easy to close a modal window without a need to create a > dedicated controller.

我的確嘗試過這個伯爵更呃,但我猜想要麼是其他的干擾,要麼我沒有清除我的緩存。