我有这样的代码:
.directive('hostelGridBed', function($rootScope){
return {
restrict: 'E',
scope: {
config: '=',
range: '=',
days: '='
},
link: function(scope, element){
},
controller: ['$scope', function($scope){
$scope.innerStay = '';
function switchStay(data){
$rootScope.$broadcast('SwitchStay', {data: data.config});
};
$scope.onDropComplete = function(data,evt){
//$rootScope.$broadcast
switchStay(data);
console.log('Drop completo bed!');
}
}],
templateUrl: './js/templates/hostelgridbed.html'
}
}).directive('hostelGridDay', function(StaysFactory){
return {
restrict: 'E',
scope: {
config: '=',
date: '='
},
link: function(scope,element){
},
controller: ['$scope','$rootScope', function($scope,$rootScope){
$scope.switchStay = function(evt, data){
console.log(data);
// Here. How can I access this controller's $scope
}
$scope.$on('SwitchStay', $scope.switchStay);
}],
templateUrl: './js/templates/hostelgridday.html'
}
})我从$broadcast那里得到了数据,一切都很好。除了我需要从$scope.switchStay函数中访问指令的$scope之外。有没有办法做到这一点?
发布于 2017-06-27 04:56:16
实际上,当您从指令中$broadcast或$emit事件时,您也可以将指令的作用域作为参数发送。它应该是有效的,但这实际上很糟糕,而且是一个非常糟糕的做法。
在这些情况下,您需要做的是向指令(&)添加一个回调参数,并提供switchStay函数作为回调,然后简单地从指令中触发它,传递您需要在函数内部访问的参数。
我希望这是有意义的。
https://stackoverflow.com/questions/44768379
复制相似问题