当前位置:   article > 正文

在AngularJs中使用监听(addEventListener)_angular addeventlistener

angular addeventlistener

使用范围:子类.js给父类.js或者其他页面上.js传值


二话不说,先上代码

1、Notifications.js

  1. angular.module('pr.services')
  2. .provider('Notifications',
  3. function() {
  4. //https://github.com/trochette/Angular-Design-Patterns-Best-Practices/blob/master/js/base/EventDispatcher.js
  5. var eventDispatcher = {
  6. _listeners: {},
  7. _cache: {},
  8. event: {
  9. // account: object
  10. ACCOUNT_CHANGE: 'PR_ACCOUNT_CHANGE',
  11. //Risk Measures
  12. RISK_MEASURE_CHANGE: 'PR_RISK_MEASURE_CHANGE',
  13. },
  14. addEventListener: function(type, listener, cache) {
  15. if (!this._listeners[type]) {
  16. this._listeners[type] = [];
  17. }
  18. this._listeners[type].push(listener);
  19. if (cache && this._cache.hasOwnProperty(type)) {
  20. listener.apply(null, this._cache[type]);
  21. }
  22. },
  23. removeEventListener: function(type, listener) {
  24. if (this._listeners[type]) {
  25. var index = this._listeners[type].indexOf(listener);
  26. if (index !== -1) {
  27. this._listeners[type].splice(index, 1);
  28. }
  29. }
  30. },
  31. dispatchEvent: function() {
  32. var listeners;
  33. var data = arguments;
  34. var type = arguments[0];
  35. if (typeof type !== 'string') {
  36. console.warn('EventDispatcher', 'First params must be an event type (String)');
  37. } else {
  38. // console.log(data);
  39. listeners = this._listeners[type];
  40. this._cache[type] = data;
  41. if (listeners) {
  42. listeners.forEach(function(listener) {
  43. listener.apply(null, data);
  44. });
  45. }
  46. }
  47. },
  48. getLastCalledValue: function(type) {
  49. return this._cache.hasOwnProperty(type) ? this._cache[type] : [];
  50. }
  51. };
  52. eventDispatcher.notify = eventDispatcher.dispatchEvent;
  53. this.$get = function() {
  54. return eventDispatcher;
  55. };
  56. }
  57. );


介绍一下代码:

a. eventDispatcher.notify 函数 ----------- 当数据有改变的时候,通知riskMeasureHandler函数

使用:

<span style="white-space:pre">		</span>Notifications.notify(Notifications.event.RISK_MEASURE_CHANGE,$scope.riskMeasureAll);


b.removeEventListener函数 -------------- 在scope被移除时移除监听

使用:

  1. $scope.$on('$destroy', function() {
  2. Notifications.removeEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler);
  3. });
c.addEventListener函数  -----------------  增加监听的地方,如果 notify 改变他的值就改变被改变,每次notify 每次都改变,立即改变。

使用:

  1. var riskMeasureAllAfter;
  2. var riskMeasureHandler = function(ev, riskMeasureAll) {
  3. riskMeasureAllAfter = riskMeasureAll;
  4. };
  5. Notifications.addEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler, true);
  6. $scope.$on('$destroy', function() {
  7. Notifications.removeEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler);
  8. });

d.RISK_MEASURE_CHANGE  --------  监听的事件名,唯一标识每一个事件。(在eventDispatcher 的 event 中定义)

定义:

RISK_MEASURE_CHANGE: 'PR_RISK_MEASURE_CHANGE',
使用:
Notifications.notify(Notifications.event.RISK_MEASURE_CHANGE,$scope.riskMeasureAll);


注意:

当值回传时:对象不只是值相同,而且应该是同一个对象,先用_.find 找到

  1. $scope.riskMeasureBefore = _.find($scope.riskMeasures, {
  2. name: st.getRiskMeasure()
  3. });
  4. if(!riskMeasureAllAfter){
  5. $scope.riskMeasure = $scope.riskMeasureBefore;
  6. }
  7. else{
  8. //have same data ,but not the same object
  9. $scope.riskMeasure = _.find($scope.riskMeasures, {
  10. name: riskMeasureAllAfter.changed.name
  11. });
  12. }






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/222909
推荐阅读
相关标签
  

闽ICP备14008679号