Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 从$scope内访问$scope。$on_Angularjs_Angular Directive_Angular Broadcast - Fatal编程技术网

Angularjs 从$scope内访问$scope。$on

Angularjs 从$scope内访问$scope。$on,angularjs,angular-directive,angular-broadcast,Angularjs,Angular Directive,Angular Broadcast,我有以下代码: .directive('hostelGridBed', function($rootScope){ return { restrict: 'E', scope: { config: '=', range: '=', days: '=' }, link: function(scope, element){ }, controller: ['$scope', function($scope){

我有以下代码:

.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'
  }
})
我从$broadcast获取数据,好的,一切都很好。除了我需要从$scope.switchStay函数中访问指令的$scope之外。
有没有办法做到这一点?

实际上,当您
$broadcast
$emit
指令中的事件时,您还可以将指令的范围作为参数发送。这应该行得通,但实际上这是一种糟糕的做法

在这些情况下,您需要做的是向指令(
&
)添加一个回调参数,并将
switchStay
函数作为回调函数提供,然后从指令触发它,并在函数内部传递您需要访问的参数


我希望它有意义。

我引用的代码在指令的$scope内。我需要从switchStay中访问相同的$scope。我刚才回答时认为您所说的不是相同的
$scope
。你的问题是什么?在函数内部,您只需使用
$scope
即可访问它……不,您不能。函数内没有作用域。你说“函数内没有作用域”是什么意思?@Pablo:你错了;在您给出的代码中,您确实可以简单地引用相同的
$scope
变量。问题是$broadcast的指令与$scope.$on的指令不同。在这种情况下如何更好呢?这正是使用
&
参数作为回调时的情况刚刚更新了代码。我不明白你的意思
.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'
  }
})