国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

javascript - angularjs 想寫一個簡單的toast,如何實現?

瀏覽:111日期:2023-05-06 09:14:42

問題描述

思路是使用directive來實現,但卡在不知道怎么暴露API給controller

我想彈出toast的時候在controller里調用API xxx.showToast,但不知道怎么才能取到這個接口,directive也不能作為依賴注入,卡在這里了,望指教。

問題解答

回答1:

你directive接過去寫就可以直接使用了

css代碼

.toast-box{

position:absolute;top:45%;z-index:99;max-height:250px;overflow-y:auto;margin:0 auto;float:left;left:50px;right:50px;text-align:center;

}.toast-top{

top:0;

}.toast-bottom{

top:auto;bottom:0;

}.toast-box .toast-item{

display:inline-block;margin-top:5px;padding:0 20px;max-width:100%;height: 40px;line-height: 40px;color:#fff;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;border-radius:6px;font-size: 14px;background-color: rgba(0, 0, 0, 0.8);

}.toast-box .toast-item.toast-success{

background-color: rgba(40,165,76, 0.8);

}.toast-box .toast-item.toast-error{

background-color: rgba(217,83,79, 0.8);

}.toast-box .toast-item.toast-warn{

background-color: rgba(240,173,78, 0.8);

}.toast-box .toast-item.toast-info{

background-color: rgba(3,155,229, 0.8);

}

directive代碼

angular.module(’app’).directive(’toast’, function() {

return { restrict: ’E’, require: ’ngModel’, scope: {ngModel: ’=’ }, link: function (scope, element, attr, ctrl) {/* * Read before you modify * this is a Object Sample : {text:'prompt content',type:'prompt style',timeout:'display time',max:'Display maximum number'} * If you need to add attributes! Please update the Object Sample*/var objSample = { text: 'prompt content', type: 4, timeout: 3000, max: 3 };var position = attr.position||’center’;$(’.toast-’+position).remove();var box = $(’<p class='toast-box toast-’ + position + ’'></p>’).appendTo(’body’);scope.$watch(’ngModel’, function (newValue) { if (!newValue) return;var value;if (angular.isString(newValue)) { value = { text: newValue };} else { value = angular.copy(newValue);}var timeout = isNaN(value.timeout) ? objSample.timeout : value.timeout;if (value.text != undefined && value.text != '') { var isMax = box.find('p').length >= (value.max || objSample.max) if (isMax) return; //var item = $(’<p class='toast-item toast-’ + getToastClass(value.type) + ’ animated fadeInDown'>’ + value.text + ’</p><br/>’).appendTo(box); var item = $(’<p class='toast-item toast-’ + getToastClass(value.type) + ’'>’ + value.text + ’</p><br/>’).appendTo(box); setTimeout(function () {//item.addClass(’fadeOutUp’);setTimeout(function () { item.remove();}, 500); }, timeout);}}); }};

});

function getToastClass(type) {

var toast_class;switch (type){ case 1:toast_class = 'success';break; case 2:toast_class = 'error';break; case 3:toast_class = 'warn';break; case 4:toast_class = 'info';break; default:toast_class = 'undefined';break;}return toast_class

}

html使用

<toast ng-model='toast' position='center'></toast>

控制器使用

$scope.toast = { text: 'Hellow', type: 1, timeout: 1000,max:2 };

回答2:

可以使用AngularJS-Toasterhttps://github.com/jirikavi/A...

回答3:

樓上說的angularjs-toaster挺好用的,可以用用。或者寫個service,通過di來使用。

回答4:

之前用過sweet alert,感覺也還行。http://t4t5.github.io/sweetal...

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 国产va精品网站精品网站精品 | 国产欧美另类久久精品91 | 91视频国产91久久久 | 日韩欧美在 | 国产一级毛片视频 | 成年人免费小视频 | 亚洲一区二区精品 | 久热香蕉精品视频在线播放 | 成人区精品一区二区不卡亚洲 | 国产乱子伦视频大全 | 久久久久99精品成人片三人毛片 | 手机看片精品高清国产日韩 | 欧美一级特黄aa大片在线观看免费 | 亚洲精品中文字幕一区在线 | 一级片一区 | 手机免费看a | 欧美人成毛片在线播放 | 欧美日韩精品一区三区 | 成年人视频在线免费看 | 欧美一级高清视频在线播放 | 国产精品日产三级在线观看 | 一区二区三区 亚洲区 | 日本www视频 | 久久免费影院 | 国产精品99| 午夜影院啪啪 | 在线观看国产精品日本不卡网 | 女让张开腿让男人桶视频 | 国产精品色午夜视频免费看 | 日本免费人成黄页在线观看视频 | 国产国语对白一级毛片 | 一本久道综合久久精品 | 日韩a级毛片免费视频 | 欧美三级在线 | 日韩欧美成末人一区二区三区 | 一区二区三区精品国产 | 久久久久久国产精品视频 | 欧美影院网站视频观看 | 亚州a| 国产一级一级一级成人毛片 | 国产看午夜精品理论片 |