AngularJS:从子指令访问父范围

AngularJS:从子指令访问父范围,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,以下代码不起作用。显然,我无法从child dir访问someFunction() 这是从子指令访问父范围的问题吗?当child指令来自外部库时,如何执行 角度/HTML: <parent-dir ng-controller="parentCtrl"> <child-dir ng-click="someFunction()"> </child-dir> </parent-dir> 问题是您的子目录已经从父目录创建了一个隔离的作用域 在指令

以下代码不起作用。显然,我无法从
child dir
访问
someFunction()

这是从子指令访问父范围的问题吗?当child指令来自外部库时,如何执行

角度/HTML:

<parent-dir ng-controller="parentCtrl">
  <child-dir ng-click="someFunction()">
  </child-dir>
</parent-dir>

问题是您的
子目录
已经从
父目录
创建了一个隔离的作用域

在指令声明中,如果将作用域指定为等于true,则可以访问父作用域。你会这样做:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

问题是您的
子目录
已经从
父目录
创建了一个隔离的作用域

在指令声明中,如果将作用域指定为等于true,则可以访问父作用域。你会这样做:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

问题是您的
子目录
已经从
父目录
创建了一个隔离的作用域

在指令声明中,如果将作用域指定为等于true,则可以访问父作用域。你会这样做:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

问题是您的
子目录
已经从
父目录
创建了一个隔离的作用域

在指令声明中,如果将作用域指定为等于true,则可以访问父作用域。你会这样做:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

您需要在此处提供您的指令。很可能您使用的是一个隔离作用域,它打破了作用域的父子链。我猜你有这样的想法:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];
要解决此问题,您可以直接访问父控制器,如下所示:

angular.module('module').directive('childDir', [function () {
  return {
    require: '^parentCtrl',
    link: function ($scope, $element, $attrs, parentCtrl) {
      $scope.someFunction = parentCtrl.someFunction;  // of course this only works if you make someFunction a public function on the parentCtrl
    },
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

或者,您可以通过在指令定义中不返回“scope”键或将其设置为{scope:true}(这将为您提供一个新的子范围),使您的范围非隔离。另一种选择是通过直接访问父作用域(而不是依赖原型继承)打破隔离屏障,如:$scope.$parent.someFunction()。

您需要在此处提供指令。很可能您使用的是一个隔离作用域,它打破了作用域的父子链。我猜你有这样的想法:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];
要解决此问题,您可以直接访问父控制器,如下所示:

angular.module('module').directive('childDir', [function () {
  return {
    require: '^parentCtrl',
    link: function ($scope, $element, $attrs, parentCtrl) {
      $scope.someFunction = parentCtrl.someFunction;  // of course this only works if you make someFunction a public function on the parentCtrl
    },
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

或者,您可以通过在指令定义中不返回“scope”键或将其设置为{scope:true}(这将为您提供一个新的子范围),使您的范围非隔离。另一种选择是通过直接访问父作用域(而不是依赖原型继承)打破隔离屏障,如:$scope.$parent.someFunction()。

您需要在此处提供指令。很可能您使用的是一个隔离作用域,它打破了作用域的父子链。我猜你有这样的想法:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];
要解决此问题,您可以直接访问父控制器,如下所示:

angular.module('module').directive('childDir', [function () {
  return {
    require: '^parentCtrl',
    link: function ($scope, $element, $attrs, parentCtrl) {
      $scope.someFunction = parentCtrl.someFunction;  // of course this only works if you make someFunction a public function on the parentCtrl
    },
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];

或者,您可以通过在指令定义中不返回“scope”键或将其设置为{scope:true}(这将为您提供一个新的子范围),使您的范围非隔离。另一种选择是通过直接访问父作用域(而不是依赖原型继承)打破隔离屏障,如:$scope.$parent.someFunction()。

您需要在此处提供指令。很可能您使用的是一个隔离作用域,它打破了作用域的父子链。我猜你有这样的想法:

directive("child-dir", [
    function () {
        return {
            restrict: 'A',
            scope: true,
            link: function(scope, elem, attrs){
                  }
            };
       }
]);
angular.module('module').directive('childDir', [function () {
  return {
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];
要解决此问题,您可以直接访问父控制器,如下所示:

angular.module('module').directive('childDir', [function () {
  return {
    require: '^parentCtrl',
    link: function ($scope, $element, $attrs, parentCtrl) {
      $scope.someFunction = parentCtrl.someFunction;  // of course this only works if you make someFunction a public function on the parentCtrl
    },
    scope: {
      // Having scope defined as an object makes it an 'isolate' scope
      // and breaks the chain between this scope and the parent scope.
    }
  };
}];
或者,您可以通过在指令定义中不返回“scope”键或将其设置为{scope:true}(这将为您提供一个新的子范围),使您的范围非隔离。另一种选择是通过直接访问父作用域(而不是依赖原型继承)打破隔离屏障,如:$scope.$parent.someFunction()