最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display toaster message in Angularjs? - Stack Overflow

programmeradmin1浏览0评论

Hello I am developing Angularjs Application. I am trying to display Toaster messages in my angular controller. I refereed /. I am facing below issue. I am not able to call success,info etc notification from controller and i am getting annot read property 'success' of undefined error. I have tried as below.

In index.html i have added below code.

<!--Toaster-->
    <script src=".tpls.js"></script>
    <link rel="stylesheet" href=".css" />
    <!--Toaster-->  

this is my 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);
    };
}]);

May I know why I am facing issues here? any help would be appreciated. thank you

Hello I am developing Angularjs Application. I am trying to display Toaster messages in my angular controller. I refereed http://angular-js.in/angular-toastr/. I am facing below issue. I am not able to call success,info etc notification from controller and i am getting annot read property 'success' of undefined error. I have tried as below.

In index.html i have added below code.

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

this is my 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);
    };
}]);

May I know why I am facing issues here? any help would be appreciated. thank you

Share Improve this question asked Apr 11, 2017 at 4:23 Niranjan GodboleNiranjan Godbole 2,1858 gold badges50 silver badges98 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

add toastr as string dependency to the controller.

change this

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

to this

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

You are missing toastr in controller definition.

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

Try this

<html>
<head>
	<script Src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.6.1/angular.js"></script>
	<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.0/jquery.min.js"></script>
	<script src="http://cdnjs.cloudflare./ajax/libs/toastr.js/1.3.1/js/toastr.js"></script>
	<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare./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>

发布评论

评论列表(0)

  1. 暂无评论