Javascript 使用$emit from指令向控制器发送事件
我试图在选择某个项目时,使用Javascript 使用$emit from指令向控制器发送事件,javascript,angularjs,javascript-events,Javascript,Angularjs,Javascript Events,我试图在选择某个项目时,使用$emit将事件从指令发送到控制器。我有两个用于组织的更新功能,另一个用于人员。我的指令应该指定应该发出哪个事件 这是我的更新功能 //为了组织 $scope.updateOrgs = function(selectedVal) { } //为人们 $scope.updatepeople = function(selectedVal, type) { } 如果是人,我的指令应该为updatepeople()引发一个emit事件,如果是org,它应该引发up
$emit
将事件从指令发送到控制器。我有两个用于组织的更新功能,另一个用于人员。我的指令应该指定应该发出哪个事件
这是我的更新功能
//为了组织
$scope.updateOrgs = function(selectedVal) {
}
//为人们
$scope.updatepeople = function(selectedVal, type) {
}
如果是人,我的指令应该为updatepeople()
引发一个emit事件,如果是org,它应该引发updateorg()
我的指令看起来像
.directive('search', function ($timeout) {
return {
restrict: 'AEC',
scope: {
model: '=',
searchobj: '@',
},
link: function (scope, elem, attrs, index) {
scope.handleSelection = function (selectedItem) {
scope.model = selectedItem;
scope.searchModel="";
scope.current = 0;
scope.selected = true;
$timeout(function () {
scope.onSelectupdate();
}, 200);
};
scope.Delete = function (index) {
scope.selectedIndex = index;
scope.delete({ index: index });
};
scope.Search = function (searchitem,event,searchobj) {
// alert('item entered'+name)
scope.searching = searchitem;
scope.searchobject = searchobj;
scope.onSearch({ searchitem: searchitem , searchobj:searchobj});
};
scope.current = 0;
scope.selected = true;
scope.isCurrent = function (index) {
return scope.current == index;
};
scope.setCurrent = function (index) {
scope.current = index;
};
},
controller: ['$scope','$element','$rootScope','SearchOrg', function($scope,$element,$rootScope,SearchOrg) {
$scope.searchItem = function(filter,searchobj){
//alert('search'+searchobj);
SearchOrg().fetch({'filter': filter, 'searchType': searchobj}).$promise.then(function(value){
$scope.searchData = value.data;
console.info($scope.searchData);
},
function(err) {
});
}
}],
templateUrl: TAPPLENT_CONFIG.HTML_ENDPOINT[0] + 'home/genericsearch.html'
}
});;
HTML代码段
<search searchobj=“tei-org” selectedItems=“arrayofIds” search-id=”someidtoIdentify”/>
如何执行此操作这两个函数位于不同的控制器中,而且我需要使用
$emit
?
我猜您的其他控制器不是家长,所以请使用$broadcast
查看第二个选项
var-app=angular.module('app',[]);
app.controller('firstController',函数($scope){
$scope.selectedOrgs=[]
$scope.$on('updateorgs',函数(evt,数据){
$scope.selectedOrgs.push(数据);
});
});
应用控制器('secondController',函数($scope){
$scope.selectedPeople=[]
$scope.$on('updatepeople',函数(evt,数据){
$scope.selectedPeople.push(数据);
});
});
app.directive('someDirective',function($rootScope){
返回{
作用域:{},
链接:功能(范围){
scope.options=[{
id:1,
标签:“组织a”,
键入:“组织”
}, {
id:2,
标签:“组织b”,
键入:“组织”
}, {
id:3,
标签:“人a”,
类型:'人'
}, {
id:4,
标签:“人员b”,
类型:'人'
}];
scope.changed=函数(){
如果(选择范围){
var updatetype=scope.selected.type;
如果(updatetype==='person'){
$rootScope.$broadcast('updatepeople',scope.selected);
}else if(updatetype==='org'){
$rootScope.$broadcast('updateorgs',scope.selected);
}
}
};
},
模板:“选择”
};
});代码>
组织:
{{selectedOrgs}
人:
{{selectedPeople}}
组织和人员的这两种方法在哪里定义?在不同的控制器中