2017-09-02 29 views
0

我真的有這個問題掙扎,我不知道問題出在哪兒?

我要離開了很多的代碼,但這基本上就是它的基石...基於關閉Angular material design guidelines這個應該工作。我無法弄清楚爲什麼它沒有。

當我運行的代碼,我只是得到一個錯誤: TypeError: Cannot read property 'show' of undefined

的index.html:

<!DOCTYPE html> 
<html lang="en" ng-app="Main"> 
<!-- some stuff here --> 
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0-rc4/angular-material.min.css"> 
</head> 

<body ng-controller="MainCtrl as ctrl"> 
<div ng-click="executeToast()"> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-aria.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-messages.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-sanitize.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0-rc4/angular-material.min.js"></script> 
</body> 
</html> 

控制器:

angular.module('MainController', [ 
     'MainService', 
     'MainDirective', 
     'ngMaterial', 
     'ngMessages', 
     'ngSanitize' 
    ]) 
    .controller('MainCtrl', [ '$scope', '$sce', '$mdToast', function($scope, Main, $sce, $apply, $rootScope, $mdToast) { 
     $scope.executeToast() = function() { 
      $mdToast.show($mdToast.simple({ 
       hideDelay: 3000, 
       position: 'top left', 
       content: 'testing', 
       toastClass: 'success' 
      })); 
     } 
}]); 

任何想法或可能的解決方案?謝謝!

回答

2

幾個錯誤你發在這裏:

1,在依賴注入順序必須是相同的,檢查更here

2,無需通過$apply作爲依賴,

試試這個

.controller('MainCtrl', [ '$scope', '$rootScope', '$mdToast','$sce', function($scope, $rootScope, $mdToast, $sce) { 
     $scope.executeToast() = function() { 
      $mdToast.show($mdToast.simple({ 
       hideDelay: 3000, 
       position: 'top left', 
       content: 'testing', 
       toastClass: 'success' 
      })); 
+0

是的!這工作..所以我不需要我列出的其他依賴項? –

+0

@JaneDoe現在檢查我的答案 –

相關問題