【问题标题】:How to display toaster message in Angularjs?如何在Angularjs中显示烤面包机消息?
【发布时间】:2017-04-11 04:23:42
【问题描述】:

您好,我正在开发 Angularjs 应用程序。我正在尝试在我的角度控制器中显示烤面包机消息。我推荐了http://angular-js.in/angular-toastr/。我面临以下问题。我无法从控制器调用成功、信息等通知,并且我收到未定义错误的未读取属性“成功”。我已经尝试如下。

在 index.html 中我添加了以下代码。

<!--Toaster-->
    <script src="https://unpkg.com/angular-toastr/dist/angular-toastr.tpls.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/angular-toastr/dist/angular-toastr.css" />
    <!--Toaster-->  

这是我的 main.js

var app = angular.module('RoslpApp', ['pascalprecht.translate', 'ui.router', 'toastr']);
app.config(function ($stateProvider, $urlRouterProvider, $urlRouterProvider, $translateProvider, $translatePartialLoaderProvider) {
 //ui-routing states here});

app.controller('RoslpAppController', ['$scope', '$translate', function ($scope, $translate, toastr) {
    toastr.success('Hello world!', 'Toastr fun!');
    $scope.clickHandler = function (key) {

        $translate.use(key);
    };
}]);

我可以知道为什么我在这里遇到问题吗?任何帮助,将不胜感激。谢谢

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    toastr作为字符串依赖添加到控制器。

    改一下

    app.controller('RoslpAppController', ['$scope', '$translate',function ($scope, $translate, toastr) {
    

    到这里

    app.controller('RoslpAppController', ['$scope', '$translate','toastr',function ($scope, $translate, toastr) {
    

    【讨论】:

    • 我正在使用 ui 路由,所以每次我都需要将父控制器中的烤面包机作为依赖项传递吗? ——
    • 如果你想在父控制器中使用烤面包机,你需要将它作为依赖传递
    【解决方案2】:

    您在控制器定义中缺少toastr

    app.controller('RoslpAppController', ['$scope', '$translate','toastr', function ($scope, $translate, toastr) {
    

    【讨论】:

    • 我正在使用 ui 路由,所以每次我都需要在父控制器中传递烤面包机作为依赖项吗?
    • 您需要在控制器中传递烤面包机,您正在使用烤面包机。
    【解决方案3】:

    试试这个

    <html>
    <head>
    	<script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
    	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    	<script src="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/js/toastr.js"></script>
    	<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/css/toastr.css">
    
    	<script>
    		var app=angular.module("myapp", []);
    		app.controller("namesctrl", function($scope){
    
    			$(function () {
    				$('#error').click(function () {
            // make it not dissappear
            toastr.error("Noooo oo oo ooooo!!!", "Title", {
            	"timeOut": "0",
            	"extendedTImeout": "0"
            });
        });
    				$('#info').click(function () {
       		// title is optional
       		toastr.info("Info Message", "Title");
       	});
    				$('#warning').click(function () {
    					toastr.warning("Warning");
    				});
    				$('#success').click(function () {
    					toastr.success("YYEESSSSSSS");
    				});
    			});
    
    
    		});
    
    		
    	</script>
    </head>
    <body ng-app="myapp" ng-controller="namesctrl">
    	<input type="button" value="Error" id="error" />
    	<input type="button" value="Info" id="info" />
    	<input type="button" value="Warning" id="warning" />
    	<input type="button" value="Success" id="success" />
    	<br><br><br><br>
    	See official example: <a href='http://codeseven.github.io/toastr/demo.html' target='blank'>Here</a>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2019-03-29
      • 1970-01-01
      • 2017-12-31
      • 2019-08-05
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多