angularjs 广播事件怎么把json数据传给父级元素
1个回答
展开全部
1. 源代码发现:)至少1.0.4发布完整代码短
/**
* @param {string} name Event name to listen on.
* @param {function(event)} listener Function to call when the event is emitted.
* @returns {function()} Returns a deregistration function for this listener.
*/
$on: function(name, listener) {
var namedListeners = this.$$listeners[name];
if (!namedListeners) {
this.$$listeners[name] = namedListeners = [];
}
namedListeners.push(listener);
return function() {
namedListeners[indexOf(namedListeners, listener)] = null;
};
},
情况需要返函数
var offCallMeFn = $scope.$on("onViewUpdated", callMe);
//this will deregister that listener
offCallMeFn();
2.
编辑:做确@LiviuT答
您随延角范围允许您删除侦听器所示:
//A little hack to add an $off() method to $scopes.
(function () {
var injector = angular.injector(['ng']),
rootScope = injector.get('$rootScope');
rootScope.constructor.prototype.$off = function(eventName, fn) {
if(this.$$listeners) {
var eventArr = this.$$listeners[eventName];
if(eventArr) {
for(var i = 0; i < eventArr.length; i++) {
if(eventArr[i] === fn) {
eventArr.splice(i, 1);
}
}
}
}
}
}());
面何工作:
function myEvent() {
alert('test');
}
$scope.$on('test', myEvent);
$scope.$broadcast('test');
$scope.$off('test', myEvent);
$scope.$broadcast('test');
行plunker
3.
调试代码我创建自函数像blesh答我做
MyModule = angular.module('FIT', [])
.run(function ($rootScope) {
// Custom $off function to un-register the listener.
$rootScope.$off = function (name, listener) {
var namedListeners = this.$$listeners[name];
if (namedListeners) {
// Loop through the array of named listeners and remove them from the array.
for (var i = 0; i < namedListeners.length; i++) {
if (namedListeners[i] === listener) {
return namedListeners.splice(i, 1);
}
}
}
}
});
所通我函数$ rootscope现提供给我所控制器
我代码我做
$scope.$off("onViewUpdated", callMe);
谢谢
编辑:AngularJS式做 CodeGo.net@LiviuT答想取消注册该侦听器另范围并想远离创建本变量保持登记注册函数引用能解决案
4.
代码工作
$rootScope.$$listeners.nameOfYourBind=[];
/**
* @param {string} name Event name to listen on.
* @param {function(event)} listener Function to call when the event is emitted.
* @returns {function()} Returns a deregistration function for this listener.
*/
$on: function(name, listener) {
var namedListeners = this.$$listeners[name];
if (!namedListeners) {
this.$$listeners[name] = namedListeners = [];
}
namedListeners.push(listener);
return function() {
namedListeners[indexOf(namedListeners, listener)] = null;
};
},
情况需要返函数
var offCallMeFn = $scope.$on("onViewUpdated", callMe);
//this will deregister that listener
offCallMeFn();
2.
编辑:做确@LiviuT答
您随延角范围允许您删除侦听器所示:
//A little hack to add an $off() method to $scopes.
(function () {
var injector = angular.injector(['ng']),
rootScope = injector.get('$rootScope');
rootScope.constructor.prototype.$off = function(eventName, fn) {
if(this.$$listeners) {
var eventArr = this.$$listeners[eventName];
if(eventArr) {
for(var i = 0; i < eventArr.length; i++) {
if(eventArr[i] === fn) {
eventArr.splice(i, 1);
}
}
}
}
}
}());
面何工作:
function myEvent() {
alert('test');
}
$scope.$on('test', myEvent);
$scope.$broadcast('test');
$scope.$off('test', myEvent);
$scope.$broadcast('test');
行plunker
3.
调试代码我创建自函数像blesh答我做
MyModule = angular.module('FIT', [])
.run(function ($rootScope) {
// Custom $off function to un-register the listener.
$rootScope.$off = function (name, listener) {
var namedListeners = this.$$listeners[name];
if (namedListeners) {
// Loop through the array of named listeners and remove them from the array.
for (var i = 0; i < namedListeners.length; i++) {
if (namedListeners[i] === listener) {
return namedListeners.splice(i, 1);
}
}
}
}
});
所通我函数$ rootscope现提供给我所控制器
我代码我做
$scope.$off("onViewUpdated", callMe);
谢谢
编辑:AngularJS式做 CodeGo.net@LiviuT答想取消注册该侦听器另范围并想远离创建本变量保持登记注册函数引用能解决案
4.
代码工作
$rootScope.$$listeners.nameOfYourBind=[];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询