最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Angular-UI Bootstrap组件实现警报功能
时间:2022-06-25 15:55:04 编辑:袖梨 来源:一聚教程网
Angular-UI Bootstrap提供了许多组件,从流行的的Bootstrap项目移植到Angular 指令(显著的减少了代码量)。如果你计划在Angular应用中使用Bootstrap组件,我建议细心检查。话虽如此,直接使用Bootstrap,应该也是可以工作的。
Angular controller可以共享service的代码。警报就是把service代码共享到controller的很好用例之一。
Angular-UI Bootstrap文档提供了下面的例子:
view
{{alert.msg}} 
controller
function AlertDemoCtrl($scope) {
 $scope.alerts = [
  { type: 'error', msg: 'Oh snap! Change a few things up and try submitting again.' }, 
  { type: 'success', msg: 'Well done! You successfully read this important alert message.' }
 ];
 $scope.addAlert = function() {
  $scope.alerts.push({msg: "Another alert!"});
 };
 $scope.closeAlert = function(index) {
  $scope.alerts.splice(index, 1);
 };
}
鉴于我们要在app中的不同的控制器中创建警报,并且跨控制器的代码不好引用,我们将要把它移到service中。
alertService
'use strict';
/* services.js */
// don't forget to declare this service module as a dependency in your main app constructor!
var appServices = angular.module('appApp.services', []);
appServices.factory('alertService', function($rootScope) {
  var alertService = {};
  // create an array of alerts available globally
  $rootScope.alerts = [];
  alertService.add = function(type, msg) {
   $rootScope.alerts.push({'type': type, 'msg': msg});
  };
  alertService.closeAlert = function(index) {
   $rootScope.alerts.splice(index, 1);
  };
  return alertService;
 });
view
{{ alert.msg }} 
最后,我们需要将alertService's中的closeAlert()方法绑定到$globalScope。
controller
function RootCtrl($rootScope, $location, alertService) {
 $rootScope.changeView = function(view) {
  $location.path(view);
 }
 // root binding for alertService
 $rootScope.closeAlert = alertService.closeAlert; 
}
RootCtrl.$inject = ['$scope', '$location', 'alertService'];
我不完全赞同这种全局绑定,我希望的是直接从警报指令中的close data属性中调用service方法,我不清楚为什么不这样来实现。
现在创建一个警报只需要从你的任何一个控制器中调用alertService.add()方法。
function ArbitraryCtrl($scope, alertService) {
 alertService.add("warning", "This is a warning.");
 alertService.add("error", "This is an error!");
}
相关文章
- 星塔旅人等级怎么突破 等级突破方法 10-31
- 辉烬知世怎么配队 知世配队攻略 10-31
- 口袋斗罗大陆独孤博技能是什么 独孤博技能介绍一览 10-31
- 王者万象棋香香怎么玩 香香玩法介绍 10-31
- 王者万象棋阵容怎么搭配 阵容搭配攻略 10-31
- 王者万象棋怎么玩 新手玩法攻略 10-31
 
             
                                 
                                 
                                 
                                 
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                        