在AngularJs中使用監聽(addEventListener),angularjs

來源:互聯網
上載者:User

在AngularJs中使用監聽(addEventListener),angularjs

使用範圍:子類.js給父類.js或者其他頁面上.js傳值


二話不說,先上代碼

1、Notifications.js

angular.module('pr.services')  .provider('Notifications',    function() {      //https://github.com/trochette/Angular-Design-Patterns-Best-Practices/blob/master/js/base/EventDispatcher.js      var eventDispatcher = {        _listeners: {},        _cache: {},        event: {          // account: object          ACCOUNT_CHANGE: 'PR_ACCOUNT_CHANGE',          //Risk Measures          RISK_MEASURE_CHANGE: 'PR_RISK_MEASURE_CHANGE',        },        addEventListener: function(type, listener, cache) {          if (!this._listeners[type]) {            this._listeners[type] = [];          }          this._listeners[type].push(listener);          if (cache && this._cache.hasOwnProperty(type)) {            listener.apply(null, this._cache[type]);          }        },        removeEventListener: function(type, listener) {          if (this._listeners[type]) {            var index = this._listeners[type].indexOf(listener);            if (index !== -1) {              this._listeners[type].splice(index, 1);            }          }        },        dispatchEvent: function() {          var listeners;          var data = arguments;          var type = arguments[0];          if (typeof type !== 'string') {            console.warn('EventDispatcher', 'First params must be an event type (String)');          } else {            // console.log(data);            listeners = this._listeners[type];            this._cache[type] = data;            if (listeners) {              listeners.forEach(function(listener) {                listener.apply(null, data);              });            }          }        },        getLastCalledValue: function(type) {          return this._cache.hasOwnProperty(type) ? this._cache[type] : [];        }      };      eventDispatcher.notify = eventDispatcher.dispatchEvent;      this.$get = function() {        return eventDispatcher;      };    });


介紹一下代碼:

a. eventDispatcher.notify 函數 ----------- 當資料有改變的時候,通知riskMeasureHandler函數

使用:

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


b.removeEventListener函數 -------------- 在scope被移除時移除監聽

使用:

$scope.$on('$destroy', function() {   Notifications.removeEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler);});
c.addEventListener函數  -----------------  增加監聽的地方,如果notify 改變他的值就改變被改變,每次notify 每次都改變,立即改變。

使用:

var riskMeasureAllAfter;var riskMeasureHandler = function(ev, riskMeasureAll) {  riskMeasureAllAfter = riskMeasureAll;};Notifications.addEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler, true);$scope.$on('$destroy', function() {  Notifications.removeEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler);});

d.RISK_MEASURE_CHANGE  --------  監聽的事件名,唯一標識每一個事件。(在eventDispatcher 的 event 中定義)

定義:

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


注意:

當值回傳時:對象不只是值相同,而且應該是同一個對象,先用_.find 找到

        $scope.riskMeasureBefore = _.find($scope.riskMeasures, {          name: st.getRiskMeasure()        });        if(!riskMeasureAllAfter){          $scope.riskMeasure = $scope.riskMeasureBefore;        }        else{          //have same data ,but not the same object          $scope.riskMeasure = _.find($scope.riskMeasures, {            name: riskMeasureAllAfter.changed.name          });        }






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.