思路是使用directive来实现,但卡在不知道怎么暴露API给controller
我想弹出toast的时候在controller里调用API xxx.showToast
,但不知道怎么才能取到这个接口,directive也不能作为依赖注入,卡在这里了,望指教。
思路是使用directive来实现,但卡在不知道怎么暴露API给controller
我想弹出toast的时候在controller里调用API xxx.showToast
,但不知道怎么才能取到这个接口,directive也不能作为依赖注入,卡在这里了,望指教。
这是一个angularjs1开发的项目,里面有toast插件的封装,可以参考一下项目地址
具体路径在 public/dist/js/plugins/toast/
其中toast用directive实现
(function() {
'use strict';
angular
.module('app.plugins.toast')
.directive('toast', toast);
toast.$inject = [
'plugins.toast.service',
];
function toast(toastService) {
var directive = {
restrict: 'EA',
templateUrl: '/dist/js/plugins/toast/toast.html',
scope: {},
link: link
};
return directive;
function link(scope) {
scope.toast = toastService;
}
}
})();
用service跟controller交换数据
(function () {
'use strict';
angular.module('app.plugins.toast')
.factory('plugins.toast.service', toast);
toast.$inject = [
'$timeout'
];
function toast($timeout) {
var toasts = [];
var service = {
types: {},
add: add,
get: get,
// cancel: cancel,
// clearAll: clearAll,
// clearErrors: clearErrors,
// clearSuccesses: clearSuccesses
};
service.types = {
warning: '警告',
info: '信息',
success: '成功',
error: '错误'
};
return service;
function get() {
return toasts;
}
function add(type, msg) {
var toastIcon = {
warning: 'fa-exclamation-circle',
info: 'fa-info-circle',
success: 'fa-check-circle',
error: 'fa-exclamation-circle'
};
var toast = {
type: type,
typeMsg: service.types[type],
msg: msg,
cancel: cancel,
toastIcon: toastIcon[type],
remainSecond: 3 // 展示的时间
};
if (type == 'success') {
autoDestroy(toast);
}
toasts.push(toast);
}
function autoDestroy(toast) {
$timeout(function dismiss() {
var index = toasts.indexOf(toast);
if (index > -1) {
cancel(index);
}
}, 3000);
}
/**
* [cancel Remove a single toast.]
* @param {[type]} index [description]
* @return {[type]} [description]
*/
function cancel(index) {
toasts.splice(index, 1);
}
}
})();
10 回答11.2k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
你directive接过去写就可以直接使用了
css代码
.toast-box{
}
.toast-top{
}
.toast-bottom{
}
.toast-box .toast-item{
}
.toast-box .toast-item.toast-success{
}
.toast-box .toast-item.toast-error{
}
.toast-box .toast-item.toast-warn{
}
.toast-box .toast-item.toast-info{
}
directive代码
angular.module('app').directive('toast', function() {
});
function getToastClass(type) {
}
html使用
<toast ng-model="toast" position="center"></toast>
控制器使用
$scope.toast = { text: "Hellow", type: 1, timeout: 1000,max:2 };