【问题标题】:Toastr undefined in angularjsToastr在angularjs中未定义
【发布时间】:2014-04-08 06:26:11
【问题描述】:

我正在做教程,这部分是关于实现登录的,这应该给出关于登录成功的气球

angular.module("app").value("mvToastr", toastr);

angular.module("app").factory("mvNotifier", function(mvToastr) {
   return {
      notify: function(msg) {
          mvToastr.success(msg);
          console.log(msg);
       }
    }
});

我得到了这个,但我有点不明白。所有 .js 文件似乎都已加载。请告诉我问题出在哪里,谢谢。

TypeError: Cannot call method 'extend' of undefined
   at getOptions (http://localhost:3030/vendor/toastr/toastr.js:282:14)
   at Object.success (http://localhost:3030/vendor/toastr/toastr.js:68:17)
   at Object.notify (http://localhost:3030/app/common/mvNotifier.js:6:22)
   at http://localhost:3030/app/account/mvNavBarLoginCtrl.js:8:28
   at wrappedCallback (http://localhost:3030/vendor/angular/angular.js:11033:81)
   at wrappedCallback (http://localhost:3030/vendor/angular/angular.js:11033:81)
   at http://localhost:3030/vendor/angular/angular.js:11119:26
   at Scope.$eval (http://localhost:3030/vendor/angular/angular.js:12045:28)
   at Scope.$digest (http://localhost:3030/vendor/angular/angular.js:11871:31)
   at Scope.$apply (http://localhost:3030/vendor/angular/angular.js:12151:24)

   angular.js:9503

【问题讨论】:

标签: jquery angularjs undefined toastr


【解决方案1】:

查看toastr sources 看起来getOptions() 调用了jQuery 的$.extend() 方法。

Toastr 依赖于 jQuery,来自 github 自述文件的第一行:

toastr 是一个用于非阻塞通知的 Javascript 库。 jQuery 是必需的。目标是创建一个简单的核心库,可以 定制和扩展。

只需在您的主视图 html 中包含 jquery。

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

【讨论】:

  • 非常感谢,您说得对。我有 jquery,我知道,但路径是错误的。我有jquery/jquery.js 但我应该有jquery/dist/jquery.js @phil
  • 这对我来说没什么区别。我在 toastr.js 之前引入了 jQuery,但它仍然不起作用。 Angular 就是不喜欢它。
  • @ArminderDahul 迟到的回复我知道,但是 toastr 确实可以很好地处理 angular。您应该在 before angular 中包含 jquery,否则 angular 将本质上用其自己的“jqlite”实现覆盖 jquery。 docs.angularjs.org/misc/…
【解决方案2】:

我遇到了这个问题,事实证明 Toastr 需要 JQuery 才能工作,始终确保您在 Toastr 之前包含 JQuery,例如在 angular.json 中:

            "scripts": [
              "node_modules/jquery/dist/jquery.min.js", // Make sure this comes before
              "node_modules/toastr/build/toastr.min.js", // this one
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]

【讨论】:

    【解决方案3】:

    我在处理自动化任务然后在浏览器中加载时遇到了这个问题。我升级了浏览器同步并修复了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 2015-10-10
      相关资源
      最近更新 更多