【问题标题】:Cannot read property 'show' of undefined with $mdToast无法使用 $mdToast 读取未定义的属性“显示”
【发布时间】:2018-02-11 02:01:35
【问题描述】:

我真的在为这个问题苦苦挣扎,我不知道这是怎么回事......

我将省略很多代码,但这基本上是它的要点......基于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'
            }));
      }
}]);

有什么想法或可能的解决方案吗?谢谢!

【问题讨论】:

    标签: javascript angularjs angular-material


    【解决方案1】:

    你在这里犯了几个错误:

    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'
                }));
    

    【讨论】:

    • 是的!那行得通..所以我不需要我列出的其他依赖项吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2019-07-05
    相关资源
    最近更新 更多