在 angular-material-fullstack Yeoman 生成器中使用 Angular Material,我加入了一个toast:
var last = {
bottom: false,
top: true,
left: false,
right: true
};
$scope.toastPosition = angular.extend({},last);
function sanitizePosition() {
var current = $scope.toastPosition;
if ( current.bottom && last.top ) current.top = false;
if ( current.top && last.bottom ) current.bottom = false;
if ( current.right && last.left ) current.left = false;
if ( current.left && last.right ) current.right = false;
last = angular.extend({},current);
}
$scope.getToastPosition = function() {
sanitizePosition();
return Object.keys($scope.toastPosition)
.filter(function(pos) { return $scope.toastPosition[pos]; })
.join(' ');
};
var showSimpleToast = function() {
$mdToast.show(
$mdToast.simple()
.content('Please enter a valid stock name.')
.position($scope.getToastPosition())
.hideDelay(3000)
);
};
我能做些什么来“让这段代码不碍事”?是否值得在服务中实施?我只是不希望它在我的控制器中占用太多空间。